Python语法错误 - CSV文件输入: 我正在尝试使用CSV屏蔽测试来实现,并从masking using 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
答案 0 :(得分:0)
Word def
仅用于定义函数。
要调用函数,请使用其名称和参数,而不使用" def":
faked_values = anonimize('file.csv', 'file2.csv')
答案 1 :(得分:0)
如果查看原始定义,您将看到正确的语法。
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)
。括号中的值的数量通常应与函数定义中的标识符数量相匹配。在这里,您可以使用字符串或您定义的任何其他文字值或变量代替arg1
和arg2
。
使函数参数可选。在这种情况下,括号中的字符串文字应该以标识符和=
符号开头,如下所示:def anonymize(arg1 = 'one', arg2 = 'two')
。这将允许您在没有必要的情况下调用函数来提供所有参数。如果没有为参数赋值,则会为其分配一个您在定义中编写的默认值。有效来电将是:anonymize('me')
,anonymize()
,anonymize(arg2 = 'you')
等。
答案 2 :(得分:0)
谢谢大家。我删除了该函数,只是将输入的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)
输入:身份证,姓名,电子邮件,电话 123,戴夫杰克逊,dave.jackson @ email.com,212-121-3234
蒙面输出:123,Elizabeth Myers MD,alicia70 @ hotmail.com,212-121-3234