我有一个需要清理的CSV。它有
的记录1, a[]b[][][], c[]d[][][]
2, a[]b[]c[]d[]e, a[]b[]c[]d[]e
需要从行中删除字段中发生的任何尾随[]
,然后将这些行写入新文件。例如,在这种情况下,第1行将变为1, a[]b, c[]d
,第2行将保持不变。
据我所知:
import csv
new_rows = []
with open('input.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
new_row = [i.split(',').rstrip('[]') for i in row] #won't work since this is a list, not a string
new_rows.append(new_row)
with open('output.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(new_rows)
我知道rstrip只适用于字符串而不是列表。我对Python有点新手并且无法弄明白,有人可以帮忙吗?
答案 0 :(得分:2)
几乎就在那里,看起来只是一个错误的分裂。
import csv
new_rows = []
with open('input.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
new_row = [x.rstrip('[]') for x in row]
new_rows.append(new_row)
with open('output.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(new_rows)
输入:
1, a[]b[][][], c[]d[][][]
2, a[]b[]c[]d[]e, a[]b[]c[]d[]e
我得到以下内容:
1, a[]b, c[]d
2, a[]b[]c[]d[]e, a[]b[]c[]d[]e
答案 1 :(得分:2)
import csv
new_rows = []
with open('input.csv', 'rb') as inf:
with open('output.csv', 'wb') as outf:
reader = csv.reader(inf)
writer = csv.writer(outf)
for row in reader:
new_row = [ field.rstrip('[]') for field in row ]
writer.writerow(new_row)
在Python 2.7之前,您可以将上下文管理器与
组合成一个with
语句
import contextlib
with contextlib.nested(open('input.csv', 'rb'), open('output.csv', 'wb')) as (inf, outf):
reader = ...
在Python 2.7中,修改了with
语句本身以支持这一点:
with open('input.csv', 'rb') as inf, open('output.csv', 'wb') as outf:
虽然隐式线路延续不可用;分开两行,你必须 使用行继续符:
with open('input.csv', 'rb') as inf,\
open('output.csv', 'rb') as outf: