下面有一个文本文件,我想将其转换为csv文件。
+---------------------+--------------+---------------+
| column_date | column_id | column_desc |
+---------------------+--------------+---------------+
| 2001-01-01 00:00:00 | 12345 | abc bar |
| 2001-01-01 00:00:00 | 4567 | defg |
+---------------------+--------------+---------------+
我正在寻找的预期输出是:
column_date,column_id,column_desc
2001-01-01 00:00:00,12345,abc bar
2001-01-01 00:00:00,4567,defg
有没有通过pyparsing做到这一点的例子? 谢谢。
答案 0 :(得分:0)
import re
with open("file.csv", "r+") as myFile:
content = myFile.read()
regex = r'^\|\s+(.+)\s+\|\s+(\w+)\s+\|\s+(.+)\s+\|$'
print(content)
match = re.findall(regex, content, re.MULTILINE)
[print(line[0]+","+line[1]+","+line[2]) for line in match]
|---------------------+-----------+-------------|
| column_date | column_id | column_desc |
|---------------------+-----------+-------------|
| 2001-01-01 00:00:00 | 12345 | abc bar |
| 2001-01-01 00:00:00 | 4567 | defg |
|---------------------+-----------+-------------|
column_date ,column_id,column_desc
2001-01-01 00:00:00,12345,abc bar
2001-01-01 00:00:00,4567,defg
您可能要在打印之前删除不需要的空格