我正在嘗試編寫一個python程序來清理來自CSV文件的調查數據。 我想轉儲包含一系列空白字段的行,如以下示例中的第一行和第三行。轉儲包含空白字段序列的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()
我必須先將行轉換爲字符串才能工作。儘管如此,這種方法仍然沒有確定空白字段。 'print blanks'返回一個空字符串。 – Anjo
空格將爲空,它是該行中所有空白項目的列表。然後這些被計算用於過濾。 – timlyo