这是一个总结。其中包括如何将不同长度的list写入csv中,还有如何将排列组合获得的列表写入csv中。
如果有看不懂的地方,欢迎留言。如果要转发的话,请注明出处。
1. 将不同长度的list写入csv中
这里面有两种挺有意思的方法。
import csv
##https://stackoverflow.com/questions/42086462/how-to-write-two-lists-of-different-length-to-column-and-row-in-csv-file
list1=['a','b']
list2=[['apple','banana','grapes'],['cheery']]
with open('0706.csv','w',newline='') as f:
writer=csv.writer(f)
header="col1","col2"
writer.writerow(header)
for i1,i2 in zip(list1,list2):
writer.writerow([i1]+i2)
获得的结果为:
另外一种是只有两列:
list1=['a','b']
list2=[['apple','banana','grapes'],['cheery']]
with open('0706.csv','w',newline='') as f:
writer=csv.writer(f)
header="col1","col2"
writer.writerow(header)
for i1,i2 in zip(list1,list2):
data=','.join(i2)
writer.writerow([i1]+[data])
2. 下面这个是别人叫我帮忙写的。一开始不会怎么将排列组合获得的列表插入到csv中,所以用了pandas。但是发现用pandas会导致最后生成的exe文件太大。所以查了下还是用csv来改。事实证明生成的exe的大小相差了十倍......
想获得以下的效果。A,B,C列都是固定的,里面的数据会产生变动。D列是需要生成的数据。是由A,B,C列排列组合获得的。
需要注意的几个点:
csv中的row是以列为单位的。所以一开始把A,B,C列分别组成三个list。 然后用三个for来生成一个排列组合获得的list
for row in reader:
A.append(row[0]) #one column
B.append(row[1]) #Second column
C.append(row[2])
for a in range(len(A)):
for c in range(len(C)):
for b in range(len(B)):
##Values combinations from three columns
d=list(itertools.combinations((A[a],B[b],C[c]),3))
result.append(d)
然后去掉其中有空元素的列表,这里用了,如果''不在这个列表中,就获得一个新列表。然后再用set()来获得一个不重复的列表。
这里卡了我很长时间的一个点来了。由于获得的列表会比A,B,C三列的最长长度都要长。所以用一般的zip(),或者是row.append()是不可以的。这时候需要生成一个大列表,将前三列的数据与最后的data都append进去。最后使用zip_longest()来写入csv。
longlist=[]
longlist.append(A)
longlist.append(B)
longlist.append(C)
longlist.append(final)
print(longlist)
##Get the longest length of the list and write it to the new csv
for values in zip_longest(*longlist):
writer.writerow(values)
下面是完整的代码。
import csv
import itertools
from itertools import zip_longest
A=[]
B=[]
C=[]
import os
def remove(file1):
os.remove(file1)
os.rename("test1.csv",file1)
def csvgetvalue(file1):
with open("test1.csv", 'w', newline='') as fout, open(file1) as fin:
writer = csv.writer(fout,delimiter=',',lineterminator='\n')
reader = csv.reader(fin)
result=[]
for row in reader:
A.append(row[0]) #one column
B.append(row[1]) #Second column
C.append(row[2])
for a in range(len(A)):
for c in range(len(C)):
for b in range(len(B)):
##Values combinations from three columns
d=list(itertools.combinations((A[a],B[b],C[c]),3))
result.append(d)
w=[]
for i in range(len(result)):
w+=result[i]
ww=[]
##Remove a list which has a null value
for i in range(len(w)):
if '' not in w[i]:
ww.append(w[i])
ee=[]
print(ww)
for i in range(len(ww)):
##Change the[[(xx,xx,xx),(xx.xx.xx)] to [xx.xx]
e=''.join(ww[i])
print(e)
# e=''.join(e)
ee.append(e)
print(ee)
#Change it to a set which won't have duplicate values
final=list(set(ee))
##Append the existing three columns and the new column data to the "longlist"
longlist=[]
longlist.append(A)
longlist.append(B)
longlist.append(C)
longlist.append(final)
##Get the longest length of the list and write it to the new csv
for values in zip_longest(*longlist):
writer.writerow(values)
file1=input("Your file name is: ")
csvgetvalue(file1)
remove(file1)
谢谢。
希望对你有帮助。