python 写csv文件

时间:2022-07-18 21:07:52
一、只有一列内容:
def create_file(self, a, b):
# 上传csv 文件
# os.remove('openfile.csv')
open_file = open('50000file.csv', 'w+')
open_file.write('商户ID'+','+'类目'+','+'描述'+','+'变更分值'+ '\n')
for i in range(a, b):
open_file.write(str(i)+','+'商户食品安全-重大食品安全事件'+','+'因用户食用由商户经营销售的商品(食品)导致用户发生疾患并采取住院治疗(含以上)的方式进行就医的重大食品安全事件。'+','+'7'+'\n')
open_file.close()
infile = open("openfile.csv", 'r')
data_list = []
for line in infile:
data = line.rstrip().split(',')[0]
data_list.append(data)
print(data)
id_list = data_list[1:]
# os.remove('openfile.csv')
return id_list
二、多列内容:
def create_file(self, a, b):
# 上传csv 文件
# os.remove('openfile.csv')
open_file = open('50000file.csv', 'w+')
open_file.write('商户ID'+','+'类目'+','+'描述'+','+'变更分值'+ '\n')
for i in range(a, b):
open_file.write(str(i)+','+'商户食品安全-重大食品安全事件'+','+'因用户食用由商户经营销售的商品(食品)导致用户发生疾患并采取住院治疗(含以上)的方式进行就医的重大食品安全事件。'+','+'7'+'\n')
open_file.close()
infile = open("openfile.csv", 'r')
data_list = []
for line in infile:
data = line.rstrip().split(',')[0]
data_list.append(data)
print(data)
id_list = data_list[1:]
# os.remove('openfile.csv')
return id_list