Python CSV文件输入:我尝试对实现使用CSV掩蔽测试,并从伪装使用假面具获取用例。从链接中获取示例代码并尝试执行程序。但我在访问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发布于 2016-12-15 15:45:47
如果您查看原始定义,您将看到正确的语法。
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')等。发布于 2016-12-15 15:33:07
Word def仅用于定义函数。
要调用函数,请使用它的名称和参数,不要使用"def":
faked_values = anonimize('file.csv', 'file2.csv')发布于 2016-12-15 15:49:02
谢谢各位。我已经删除了该函数,并将输入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,name,电子邮件,电话123,dave jackson,dave.jackson@email.com,212-121-3234
蒙面输出: 123,Elizabeth Myers MD,alicia70 70@hotmail.com,212-121-3234
https://stackoverflow.com/questions/41167535
复制相似问题