0

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

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
brianpck
  • 8,084
  • 1
  • 22
  • 33
saran
  • 3
  • 6
  • 3
    Yes, that line is not Python syntax. What would you expect it to do? – Matthias Dec 15 '16 at 15:23
  • I'm sorry, I dont understand, can you please explain. How can I pass the input csv file into the function anonymize.I would like to pass the input and out CSV files to mask the data. I was able to read from a file using - " with open('masktest.csv', 'rU') as f: reader=csv.DictReader(f) for row in reader: print(row['name']) " but the function doesnt work. – saran Dec 15 '16 at 15:33
  • Your indentation is incorrect, please, fix it – illright Dec 15 '16 at 15:35

3 Answers3

0

Word def is used only for defining function.

To call a function use its name and parameters without "def":

faked_values = anonimize('file.csv', 'file2.csv')
Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
  • Thank you Eugene. I have just passed the variables with the values in the definition itself. ex: def anonymize(source='masktest.csv', target='masktest_tgt.csv'): – saran Dec 15 '16 at 16:02
0

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. A call looks like this: 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.

  • 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'). 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.

illright
  • 3,991
  • 2
  • 29
  • 54
  • Thanks for your help Leva. That cleared up my program and I have passed the literal values and it worked fine. – saran Dec 15 '16 at 15:53
0

Thanks folks. I have removed the function and just passed the input csv file name as a input and it worked like a charm. 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

masked output: 123,Elizabeth Myers MD,alicia70@hotmail.com,212-121-3234

saran
  • 3
  • 6
  • Having the same problem...how did you call the function? What command did you have to execute once you had your code in this form? – 0xsegfault Apr 24 '17 at 13:44
  • Hello Data_Kid, Did you look at the sample I have given. Please check and let me know. I have just used the file name with in def ---> (with open('masktest.csv', 'rU') as f: with open('masktest_tgt.csv', 'w') as o:) – saran Apr 24 '17 at 18:45
  • It eventually worked . I had to use the csv library and not the unicode. What version of python are you running? – 0xsegfault Apr 24 '17 at 18:47
  • I'm running on python 2.7. – saran Apr 24 '17 at 18:52
  • I think thats what the issue was . The unicodecsv function will not work with this on python 3.5 and above – 0xsegfault Apr 24 '17 at 18:53