Python faker - 语法错误 - CSV 文件

Python faker - Syntax error - CSV file

Python 语法错误 - CSV 文件输入: 我正在尝试使用 CSV 屏蔽测试来实现并从 masking using faker 中选取用例。从 link 中获取示例代码并尝试执行该程序。但是访问 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,您可以使用字符串或您定义的任何其他文字值或变量。

  • 使函数参数可选。在这种情况下,括号中的字符串文字前面应该有一个标识符和一个 = 符号,例如: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,name,email,phone 123,戴夫·杰克逊,dave.jackson@email.com,212-121-3234

掩码输出:123,Elizabeth Myers MD,alicia70@hotmail.com,212-121-3234