gpt4 book ai didi

python - 转储 CSV 文件中包含一系列空白字段的行

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:57 25 4
gpt4 key购买 nike

我正在尝试编写一个 python 程序来清理来自 CSV 文件的调查数据。我想转储包含一系列空白字段的行,如以下示例中的第一行和第三行。

"1","a","b","c",,,,,
"2","a","b","c","d","e","f",,"h"
"3","a","b","c",,,,,
"4","a","z","u","d","i","f","x","h"
"5","d","c","c",,"c","f","g","z"

按照我不成功的代码:

import csv

fname = raw_input("Enter input file name: ")
if len(fname) < 1 : fname = "survey.csv"

foutput = raw_input("Enter output file name: ")
if len(foutput) < 1 : foutput = "output_"+fname


input = open(fname, 'rb')
output = open(foutput, 'wb')


searchFor = 5*['']

writer = csv.writer(output)

for row in csv.reader(input):
if searchFor not in row :
writer.writerow(row)

input.close()
output.close()

最佳答案

使用counter检查一个列表是否是另一个列表的子集,如下所示。如果你想删除空元素,那么只需使用 Noneboollen 来过滤空白并丢弃它们 -

import csv
from itertools import repeat
from collections import Counter
input = open(fname, 'rb')
output = open(foutput, 'wb')

writer = csv.writer(output)
#Helper function
def counterSubset(list1, list2):
c1, c2 = Counter(list1), Counter(list2)
for k, n in c1.items():
if n > c2[k]:
return False
return True
for row in csv.reader(input):
if not counterSubset(list(repeat('',5)),row):# i used 5 for five '' you can change it
writer.writerow(row)#use filter(None,row) or filter(bool,row) or filter(len,row) to remove empty elements
input.close()
output.close()

输出-

1,a,b,c,,
2,a,b,c,d,e,f,g,h
4,a,,z,u,d,i,f,x,h
5,d,c,c,d,c,f,g,z

关于python - 转储 CSV 文件中包含一系列空白字段的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34991082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com