我想从CSV文件中删除所有不以数字开头的行。据我了解,我可以通过创建一个新的CSV文件来做到这一点,我需要将输入中的所需行复制到输出文件中,但是当我执行代码时,输出文件将使用默认定界符分隔每个字符, case是,
,它在输出文件中的每个字符之后都带有,
的行。那是我的代码:
import os, sys, csv
input_dir = "/home/test/input/"
for csv_in in os.listdir(input_dir):
if csv_in.lower().endswith('.csv') and not csv_in.lower().startswith('fixed_'):
csv_infile = input_dir + csv_in
csv_out = input_dir + "fixed_" + csv_in
with open(csv_infile) as f, open(csv_out, 'wb') as f_out:
reader = csv.reader(f)
writer = csv.writer(f_out)
for row in f:
if row.startswith(tuple('0123456789')):
writer.writerow(row)
答案 0 :(得分:2)
尝试使用file.write
例如:
import os
input_dir = "/home/test/input/"
for csv_in in os.listdir(input_dir):
if csv_in.endswith('.csv') and not csv_in.lower().startswith('fixed_'):
csv_infile = os.path.join(input_dir, csv_in)
csv_out = os.path.join(input_dir, "fixed_" + csv_in)
with open(csv_infile) as f, open(csv_out, 'wb') as f_out:
for row in f:
if row.startswith(tuple('0123456789')):
f_out.write(row)
答案 1 :(得分:0)
CSV行为很难调试。 添加一些调试输出明确表明您应该使用“阅读器”迭代器。在这种情况下,行包含逗号分隔值的列表。然后,您必须将startswith应用于列表的第一项。
import os, sys, csv
input_dir = "/home/lc253481/src/python/"
for csv_in in os.listdir(input_dir):
if csv_in.lower().endswith('.csv') and not csv_in.lower().startswith('fixed_'):
csv_infile = input_dir + csv_in
csv_out = input_dir + "fixed_" + csv_in
with open(csv_infile, 'r') as f, open(csv_out, 'wb') as f_out:
reader = csv.reader(f, delimiter=',')
writer = csv.writer(f_out, delimiter=',')
for row in reader:
print row
print len(row)
if row[0].startswith(tuple('0123456789')):
writer.writerow(row)