简体   繁体   English

Python造假者-语法错误-CSV文件

[英]Python faker - Syntax error - CSV file

Python Syntax Error - CSV file input: I'm trying to use CSV masking test for an implementation and picked up use case from masking using faker . Python语法错误-CSV文件输入:我正在尝试使用CSV屏蔽测试进行实施,并从使用faker的屏蔽中获取用例。 Picked up the sample code from the link and trying to execute the program. 从链接中获取示例代码并尝试执行该程序。 But I'm getting syntax error when accessing the csv file. 但是访问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 is used only for defining function. Word def仅用于定义功能。

To call a function use its name and parameters without "def": 要调用一个函数,请使用其名称和参数而不带“ def”:

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

If you look at the original definition, you'll see the correct syntax. 如果您查看原始定义,则会看到正确的语法。

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...

What's different here, is that when you define a function, you must provide valid identifiers in parentheses. 此处的不同之处在于,定义函数时,必须在括号中提供有效的标识符。 An identifier is essentially a name for the variable, what you'll use to refer to the argument inside your function. 标识符本质上是变量的名称,您将用它来引用函数中的参数。

Possibly, you meant to do one of the following things: 您可能打算执行以下操作之一:

  • Call a function, not define it. 调用一个函数,而不定义它。 In that case, you shouldn't use the def keyword. 在这种情况下,您不应使用def关键字。 A call looks like this: func(arg1, arg2) . 调用看起来像这样: func(arg1, arg2) The amount of values in parentheses should generally match up with the amount of identifiers in the function definition. 括号中的值的数量通常应与函数定义中的标识符的数量匹配。 And here, in place of arg1 and arg2 , you can use strings or any other literal values or variables that you've defined. 在这里,您可以使用字符串或任何其他已定义的文字值或变量来代替arg1arg2

  • Make function arguments optional. 使函数参数为可选。 In this case, the string literals in parentheses should be preceded with an identifier and a = sign, like so: def anonymize(arg1 = 'one', arg2 = 'two') . 在这种情况下,括号中的字符串文字应带有标识符和a =符号,例如: def anonymize(arg1 = 'one', arg2 = 'two') This will allow you to call a function without a neccesity to provide all arguments. 这将允许您调用函数而无需提供所有参数。 If an argument is not given a value, it will be assigned with a default one that you wrote in the definition. 如果未给自变量赋值,则将为其分配您在定义中编写的默认值。 Valid calls will be: anonymize('me') , anonymize() , anonymize(arg2 = 'you') , etc. 有效的调用将是: anonymize('me')anonymize()anonymize(arg2 = 'you')等。

Thanks folks. 谢谢大家。 I have removed the function and just passed the input csv file name as a input and it worked like a charm. 我删除了该功能,只是将输入的csv文件名作为输入传递,它的工作原理就像一个超级按钮。 Here is the code. 这是代码。

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)

Input: id,name,email,phone 123,dave jackson,dave.jackson@email.com,212-121-3234 输入:id,姓名,电子邮件,电话123,dave jackson,dave.jackson @ email.com,212-121-3234

masked output: 123,Elizabeth Myers MD,alicia70@hotmail.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