【问题标题】:Error while using Python 3 to mask existing data using Faker使用 Python 3 使用 Faker 屏蔽现有数据时出错
【发布时间】:2017-05-21 08:47:12
【问题描述】:

我正在使用 Python 3 使用 Faker 包来屏蔽数据集。我在以下位置获得了一个代码: http://blog.districtdatalabs.com/a-practical-guide-to-anonymizing-datasets-with-python-faker

代码:

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)

但我不断收到如下错误:

C:\Anaconda3.4\lib\site-packages\spyderlib\widgets\externalshell\start_ipython_kernel.py:1: DeprecationWarning: 'U' mode is deprecated # -- 编码:utf-8 -- Traceback(最近一次调用最后一次):

文件“”,第 5 行,在 writer = csv.DictWriter(o, reader.fieldnames)

文件“C:\Anaconda3.4\lib\csv.py”,第 96 行,在字段名中 self._fieldnames = next(self.reader)

文件“C:\Anaconda3.4\lib\site-packages\unicodecsv\py3.py”,第 55 行,下一个 return self.reader.下一个()

文件“C:\Anaconda3.4\lib\site-packages\unicodecsv\py3.py”,第 51 行,在 f = (bs.decode(encoding, errors=errors) for bs in f)

AttributeError: 'str' 对象没有属性 'decode'

有人可以帮我在 Python 3 中实现代码吗?非常感谢。

【问题讨论】:

    标签: python-3.x faker data-masking


    【解决方案1】:

    对于 Python3,使用标准 csv(导入 csv)并删除 'rU' 中的 U

    【讨论】:

      【解决方案2】:

      我也花了一些时间将网上找到的 python2 faker 示例转换为 python3。下面的转换应该可以工作(非常感谢@AKhooli的回答!)

          import csv
          from faker import Faker
          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  = Faker()
      
              # Create mappings of names & emails to faked names & emails.
              c1  = defaultdict(faker.msisdn)
              c2 = defaultdict(faker.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, 'r') as f:
              with open(target, 'w', newline='') 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
                  # with header!
                  writer.writeheader()
                  for row in anonymize_rows(reader):
                      writer.writerow(row)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-16
        • 1970-01-01
        • 2021-08-26
        • 2017-08-19
        • 1970-01-01
        • 1970-01-01
        • 2021-07-23
        相关资源
        最近更新 更多