2017-05-21 1 views
0

Ich benutze Python 3, um ein Dataset mit Faker-Paket zu maskieren. Ich habe einen Code erhalten unter: http://blog.districtdatalabs.com/a-practical-guide-to-anonymizing-datasets-with-python-faker.Fehler bei der Verwendung von Python 3 zum Maskieren vorhandener Daten mit Faker

Code:

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. 
    c1 = defaultdict(faker.CARD_NO_ID) 
    c2 = defaultdict(faker.ISS_USER_NAME) 

    # Iterate over the rows and yield anonymized rows. 
    for row in rows: 
     # Replace the name and email fields with faked fields. 
     row['CARD_NO_ID'] = c1[row['CARD_NO_ID']] 
     row['ISS_USER_NAME'] = c2[row['ISS_USER_NAME']] 

     # Yield the row back to the caller 
     yield row 

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

source = 'card_transaction_data_all.csv' 
target = 'card_transaction_data_all_fake.csv' 

with open(source, 'rU') as f: 
    with open(target, '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): 
      writer.writerow(row) 

Aber ich erhalte immer Fehler wie folgt:

C: \ Anaconda3.4 \ lib \ site-packages \ spyderlib \ Widgets \ externalshell \ start_ipython_kernel.py: 1: DeprecationWarning: 'U' Modus veraltet # - - Programmierung: utf-8- - Traceback (jüngste Aufforderung zuletzt):

Datei "", Linie 5, in writer = csv.DictWriter (o, reader.fieldnames)

Datei "C: \ Anaconda3.4 \ lib \ csv.py", Linie 96, in Feldnamen self._fieldnames = next (self.reader)

Datei "C: \ Anaconda3.4 \ lib \ site-packages \ unicodecsv \ py3.py", Zeile 55, in nächste return self.reader. nächsten()

Datei "C: \ Anaconda3.4 \ lib \ site-packages \ unicodecsv \ py3.py", Zeile 51, in f = (bs.decode (Codierung Fehler = Fehler) für bs in f)

Attribute: 'str' Objekt hat kein Attribut 'decode'

Kann jemand bitte helfen Sie mir den Code in Python 3 zu implementieren? Danke vielmals.

Antwort

0

Für Python3, verwenden die Standard-CSV (Import CSV) und entfernen Sie das U in 'rU'

Verwandte Themen