我有一个CSV文件,由于,,
分隔符,每隔一列产生一个空列。我正在尝试删除这些空列,但我无法弄清楚如何循环遍历行并将,,
替换为,
。
任何通过线路循环并替换分隔符的帮助将非常感激。
谢谢!
示例数据:
Year,,2005-12,,2006-12,,2007-12,,2008-12,,2009-12,,2010-12,,2011-12,,2012-12,,2013-12,,2014-12,,TTM
Data1,,100.00,,100.00,,100.00,,100.00,,100.00,,100.00,,100.00,,100.00,,100.00,,100.00,,100.00
....
答案 0 :(得分:0)
您可以使用pysed:
从命令行执行此操作https://pypi.python.org/pypi/pysed
(stackoverflow)macbook:stackoverflow joeyoung$ pip install pysed
(stackoverflow)macbook:stackoverflow joeyoung$ cat file.csv
Year,,2005-12,,2006-12,,2007-12,,2008-12,,2009-12,,2010-12,,2011-12,,2012-12,,2013-12,,2014-12,,TTM
Data1,,100.00,,100.00,,100.00,,100.00,,100.00,,100.00,,100.00,,100.00,,100.00,,100.00,,100.00
(stackoverflow)macbook:stackoverflow joeyoung$ pysed -r ",," "," file.csv --write
(stackoverflow)macbook:stackoverflow joeyoung$ cat file.csv
Year,2005-12,2006-12,2007-12,2008-12,2009-12,2010-12,2011-12,2012-12,2013-12,2014-12,TTM
Data1,100.00,100.00,100.00,100.00,100.00,100.00,100.00,100.00,100.00,100.00,100.00
答案 1 :(得分:0)
如果你想通过python实现它,那么使用replace方法。
>>> s = 'year,,2015-12,,2006-12'
>>> print(s)
year,,2015-12,,2006-1
>>> new_s = s.replace(",,",",")
>>> print(new_s)
year,2015-12,2006-12
答案 2 :(得分:0)
使用csv
模块:
import csv
input = open('in.csv', 'rb')
output = open('out.csv', 'wb')
writer = csv.writer(output)
for read in csv.reader(input):
writer.writerow([x for x in read if x]) #Removes empty fields and writes into new file
input.close()
output.close()
虽然输入csv文件非常大,但我认为这不是一个最佳解决方案。