簡體   English   中英

Python造假者-語法錯誤-CSV文件

[英]Python faker - Syntax error - CSV file

Python語法錯誤-CSV文件輸入:我正在嘗試使用CSV屏蔽測試進行實施,並從使用faker的屏蔽中獲取用例。 從鏈接中獲取示例代碼並嘗試執行該程序。 但是訪問csv文件時出現語法錯誤。

import unicodecsv as csv
from faker import Factory
from collections import defaultdict



def anonymize_rows(rows):
"""
Rows is an iterable of dictionaries that contain name and
email fields that need to be anonymized.
"""
# Load the faker and its providers
faker  = Factory.create()

# Create mappings of names & emails to faked names & emails.
names  = defaultdict(faker.name)
emails = defaultdict(faker.email)

# Iterate over the rows and yield anonymized rows.
for row in rows:
    # Replace the name and email fields with faked fields.
    row['name']  = names[row['name']]
    row['email'] = emails[row['email']]

    # Yield the row back to the caller
    yield row


   def anonymize('masktest.csv', 'masktest_tgt.csv'):
"""
The source argument is a path to a CSV file containing data to anonymize,
while target is a path to write the anonymized CSV data to.
"""
with open('masktest.csv', 'rU') as f:
     with open('masktest_tgt.csv', 'w') as o:
        # Use the DictReader to easily extract fields
        reader = csv.DictReader(f)
        writer = csv.DictWriter(o, reader.fieldnames)

        # Read and anonymize data, writing to target file.
        for row in anonymize_rows(reader):
            print (row['name'])
            writer.writerow(row)

Traceback (most recent call last):
  File "python", line 34
    def anonymize('masktest.csv', 'masktest_tgt.csv'):
                               ^
SyntaxError: invalid syntax

Word def僅用於定義功能。

要調用一個函數,請使用其名稱和參數而不帶“ def”:

faked_values = anonimize('file.csv', 'file2.csv')

如果您查看原始定義,則會看到正確的語法。

def anonymize(source, target):
    """
    The source argument is a path to a CSV file containing data to anonymize,
    while target is a path to write the anonymized CSV data to.
    """
    # more code...

此處的不同之處在於,定義函數時,必須在括號中提供有效的標識符。 標識符本質上是變量的名稱,您將用它來引用函數中的參數。

您可能打算執行以下操作之一:

  • 調用一個函數,而不定義它。 在這種情況下,您不應使用def關鍵字。 調用看起來像這樣: func(arg1, arg2) 括號中的值的數量通常應與函數定義中的標識符的數量匹配。 在這里,您可以使用字符串或任何其他已定義的文字值或變量來代替arg1arg2

  • 使函數參數為可選。 在這種情況下,括號中的字符串文字應帶有標識符和a =符號,例如: def anonymize(arg1 = 'one', arg2 = 'two') 這將允許您調用函數而無需提供所有參數。 如果未給自變量賦值,則將為其分配您在定義中編寫的默認值。 有效的調用將是: anonymize('me')anonymize()anonymize(arg2 = 'you')等。

謝謝大家。 我刪除了該功能,只是將輸入的csv文件名作為輸入傳遞,它的工作原理就像一個超級按鈕。 這是代碼。

import csv
import unicodecsv as csv
from faker import Factory
from collections import defaultdict

def anonymize_rows(rows):
"""
Rows is an iterable of dictionaries that contain name and
email fields that need to be anonymized.
"""
# Load the faker and its providers
faker  = Factory.create()

# Create mappings of names & emails to faked names & emails.
names  = defaultdict(faker.name)
emails = defaultdict(faker.email)

# Iterate over the rows and yield anonymized rows.
for row in rows:
    # Replace the name and email fields with faked fields.
    row['name']  = names[row['name']]
    row['email'] = emails[row['email']]

    # Yield the row back to the caller
    yield row

#def anonymize('masktest.csv', 'masktest_tgt.csv'):
"""
The source argument is a path to a CSV file containing data to anonymize,
while target is a path to write the anonymized CSV data to.
"""
with open('masktest.csv', 'rU') as f:
with open('masktest_tgt.csv', 'w') as o:
        # Use the DictReader to easily extract fields
    reader = csv.DictReader(f)
    writer = csv.DictWriter(o, reader.fieldnames)

        # Read and anonymize data, writing to target file.
    for row in anonymize_rows(reader):
        print (row['name'])
        writer.writerow(row)

輸入:id,姓名,電子郵件,電話123,dave jackson,dave.jackson @ email.com,212-121-3234

屏蔽的輸出:123,Elizabeth Myers MD,alicia70 @ hotmail.com,212-121-3234

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM