new_rows = []
headers = None
with open(csv_file_name, encoding='utf-8') as f:
f_csv = csv.reader(f)
headers = next(f_csv)
for r in f_csv:
uid = r[0].strip()
new_rows.append(r)
# 清洗后的数据保存
with open(new_csv_file_name, 'w', encoding='utf-8') as f:
f_csv = csv.writer(f, lineterminator='\n')
f_csv.writerow(headers)
f_csv.writerows(new_rows)
防止多余一个空行,须添加一个关键字参数lineterminator=’\n’
读取时为了不索引序号,也可以使用命名元组:
with open(csv_file_name, encoding='utf-8') as f:
f_csv = csv.reader(f)
headers = next(f_csv)
Row = namedtuple('Row', headers)
print(Row)
for r in f_csv:
count_all += 1
row = Row(*r)
uid = row.UID
关联
文档信息
- 本文作者:zhupite
- 本文链接:https://zhupite.com/python/python-csv.html
- 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)