python写入csv文件时,指定编码格式为‘utf-8-sig’即可
from faker import Faker
import datetime
from pywin.mfc.object import Object
title = ['company', 'name', 'phone', 'sale', 'date']
class GenerateData(Object):
def __init__(self):
super().__init__()
fake = Faker('zh_CN')
self.series = [[fake.company(), fake.name(), fake.phone_number(), fake.numerify(),
fake.date_between(datetime.datetime(2019, 1, 1, 00, 00, 00), datetime.datetime.now()).strftime(
'%Y-%m-%d')]
for i in range(100)]
def write_csv(self, file_name):
pass
with open(file_name, 'w', encoding='utf-8-sig') as f:
f.write('{}{}'.format(','.join(title), '\n'))
for item in self.series:
f.write('{}{}'.format(','.join(item), '\n'))
if __name__ == '__main__':
data = GenerateData()
data.write_csv('random.csv')