Python 3.x 使用Python3使用Faker屏蔽现有数据时出错
我使用Python3使用Faker包屏蔽数据集。我在以下网址获得了一个代码: . 代码: 但我不断得到如下错误: C:\Anaconda3.4\lib\site packages\spyderlib\widgets\externalshell\start\U ipython\U kernel.py:1:deprecation警告:“U”模式已被弃用 #--编码:utf-8-- 回溯(最近一次呼叫最后一次): 文件“”,第5行,在 writer=csv.DictWriter(o,reader.fieldnames) 文件“C:\Anaconda3.4\lib\csv.py”,第96行,字段名 self.\u fieldnames=next(self.reader) 文件“C:\Anaconda3.4\lib\site packages\unicodesv\py3.py”,第55行,在next 返回self.reader.下一步() 文件“C:\Anaconda3.4\lib\site packages\unicodesv\py3.py”,第51行,在 f=(对于f中的bs,bs.decode(编码,错误=错误) AttributeError:“str”对象没有属性“decode”Python 3.x 使用Python3使用Faker屏蔽现有数据时出错,python-3.x,faker,data-masking,Python 3.x,Faker,Data Masking,我使用Python3使用Faker包屏蔽数据集。我在以下网址获得了一个代码: . 代码: 但我不断得到如下错误: C:\Anaconda3.4\lib\site packages\spyderlib\widgets\externalshell\start\U ipython\U kernel.py:1:deprecation警告:“U”模式已被弃用 #--编码:utf-8-- 回溯(最近一次呼叫最后一次): 文件“”,第5行,在 writer=csv.DictWriter(o,reader.f
有人能帮我用Python 3实现代码吗?非常感谢。对于Python3,使用标准csv(导入csv)并删除“rU”中的U我也花了一些时间将在线找到的python2伪造示例转换为Python3。下面的转换应该有效(非常感谢您的回答!)
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)
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)