今天参加了LeetCode的周赛,相对上次有了一定的进步,学习了使用python的Faker库来生成测试数据,复习了一部分操作系统的基础知识
from faker import Faker
import csv
import pandas as pd
fake = Faker(["zh_CN"])
Faker.seed(0)
def get_person():
key_list = ["姓名","电话","邮箱","地址","工作单位","职位","城市","银行卡号"]
for _ in range(20):
info_list = [fake.name(),fake.phone_number(),fake.email(),fake.address(),fake.company(),fake.job(),fake.city(),fake.credit_card_number()]
person_info = dict(zip(key_list,info_list))
persons = []
# 如果 book 条数足够多的话,pandas 会每次往文件中写 50 条数据。
persons.append(person_info)
data = pd.DataFrame(persons)
fileName = 'example.csv'
print(person_info)
# 写入csv文件,'a+'是追加模式
csv_headers = ["姓名", "电话", "邮箱", "地址", "工作单位", "职位", "城市", "银行卡号"]
data.to_csv(fileName, header=False, index=False, mode='a+', encoding='utf-8')
if __name__ == '__main__':
get_person()
from faker import Faker
import xlwt
import sys
def getInformationDict(num):
InformationDict = {}
faker = Faker(locale='zh_CN') # 添加
for _ in range(int(num)):
InformationDict[faker.name()] = getInformationList()
return storedData(InformationDict)
'''创建一个列表,列表中包含各类人物信息'''
def getInformationList():
InformationList = []
faker = Faker(locale='zh_CN')
InformationList.append(faker.phone_number())
InformationList.append(faker.year())
InformationList.append(faker.month())
InformationList.append(faker.month())
InformationList.append(faker.month())
InformationList.append(faker.month())
InformationList.append(faker.month())
return InformationList
def storedData(InformationDict):
filename = xlwt.Workbook(encoding='utf-8')
worksheet = filename.add_sheet('sheet1')
worksheet.write(0, 0, label="")
worksheet.write(0, 1, label="year_id")
worksheet.write(0, 2, label="month_id")
worksheet.write(0, 3, label="market_id")
worksheet.write(0, 4, label="旅客量")
worksheet.write(0, 5, label="客座率")
i = 1
try:
for name in InformationDict:
worksheet.write(i, 0, label=name)
worksheet.write(i, 1, label=InformationDict[name][0])
worksheet.write(i, 2, label=InformationDict[name][1])
worksheet.write(i, 3, label=InformationDict[name][2])
worksheet.write(i, 4, label=InformationDict[name][3])
worksheet.write(i, 5, label=InformationDict[name][4])
worksheet.write(i, 6, label=InformationDict[name][5])
i = i + 1
except IndexError:
return "Error:列表索引超出了取值范围"
else:
filename.save('My.csv')
return "执行成功"
if __name__ == '__main__':
# num = sys.argv[1]
print(getInformationDict(1000))