Python faker语法错误CSV fi

2024-04-27 12:34:38 发布

您现在位置:Python中文网/ 问答频道 /正文

Python语法错误-CSV文件输入: 我正在尝试对一个实现使用CSV屏蔽测试,并从datasets-with-python-faker" rel="nofollow noreferrer">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)

^{pr2}$

Tags: andcsvthetonamefieldsnamesemail
3条回答

如果您查看原始定义,就会看到正确的语法。在

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和{},您可以使用字符串或您定义的任何其他文字值或变量。

  • 使函数参数可选。在这种情况下,圆括号中的字符串文本前面应该有一个标识符和一个=符号,如这样: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,戴夫·杰克逊,戴夫。杰克逊@电子邮件,212-121-3234

屏蔽输出:123,伊丽莎白·迈尔斯总经理,alicia70@hotmail.com,212-121-3234

单词def仅用于定义函数。在

要调用函数,请使用不带“def”的名称和参数:

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

相关问题 更多 >