删除 csv 文件中的 CR/LF
remove CR/LF in a csv file
OS: Windows
工具:Python
文件:https://drive.google.com/file/d/1vVhQMbnQd8qvhZ51_91_hazBc-_7pc_9/view?usp=sharing
我有一个 csv 文件,双引号用于自由文本列,逗号分隔,反斜杠用于转义。当我使用 Pandas/Excel 读取此 csv 文件时,由于 CR/LF 我猜有些行不在位置。
在 python 中,如何删除所有 cr/lf 才能正确读取 csv 文件?我试过了,没有按预期工作。
with open("sampledata.csv", "r") as infile,\
open("outfile.csv", mode="w") as outfile:
f = infile.read()
f = f.replace("\n", " ")
outfile.write(f)
你显然没有正确设置转义字符。我可以毫无问题地阅读你的文件 (pandas 1.2.4):
import pandas as pd
pd.read_csv("sampledata.csv", escapechar="\").T
输出:
0 1
Id a0o7F00000B6E3GQAV a0o7F000007b31GQAQ
LastModifiedDate 2021-05-04 05:37:21.0000000 2020-03-13 23:23:54.0000000
Description Every student needs it. We have created our "product" which is essenti...
Age 30 25
OS: Windows
工具:Python
文件:https://drive.google.com/file/d/1vVhQMbnQd8qvhZ51_91_hazBc-_7pc_9/view?usp=sharing
我有一个 csv 文件,双引号用于自由文本列,逗号分隔,反斜杠用于转义。当我使用 Pandas/Excel 读取此 csv 文件时,由于 CR/LF 我猜有些行不在位置。
在 python 中,如何删除所有 cr/lf 才能正确读取 csv 文件?我试过了,没有按预期工作。
with open("sampledata.csv", "r") as infile,\
open("outfile.csv", mode="w") as outfile:
f = infile.read()
f = f.replace("\n", " ")
outfile.write(f)
你显然没有正确设置转义字符。我可以毫无问题地阅读你的文件 (pandas 1.2.4):
import pandas as pd
pd.read_csv("sampledata.csv", escapechar="\").T
输出:
0 1
Id a0o7F00000B6E3GQAV a0o7F000007b31GQAQ
LastModifiedDate 2021-05-04 05:37:21.0000000 2020-03-13 23:23:54.0000000
Description Every student needs it. We have created our "product" which is essenti...
Age 30 25