[英]remove CR/LF in a csv file
OS: Windows操作系统:Windows
Tool: Python工具:Python
File: https://drive.google.com/file/d/1vVhQMbnQd8qvhZ51_91_hazBc-_7pc_9/view?usp=sharing文件: https://drive.google.com/file/d/1vVhQMbnQd8qvhZ51_91_hazBc-_7pc_9/view?usp=sharing
I have a csv file, double quoted for free text column, comma delimited, back slash for escaping.我有一个 csv 文件,自由文本列的双引号,逗号分隔,escaping 的反斜杠。 Some rows out of positions when I use Pandas/Excel to read this csv file due to CR/LF I guess.当我使用 Pandas/Excel 读取这个 csv 文件时,由于 CR/LF 我猜有些行不在位置。
In python, how can I remove all the cr/lf so I can read the csv file properly?在 python 中,如何删除所有 cr/lf 以便正确读取 csv 文件? I tried this, not working as expect.我试过这个,没有按预期工作。
with open("sampledata.csv", "r") as infile,\
open("outfile.csv", mode="w") as outfile:
f = infile.read()
f = f.replace("\n", " ")
outfile.write(f)
You obviously didn't correctly set the escape character.您显然没有正确设置转义字符。 I can read your file without problems (pandas 1.2.4):我可以毫无问题地阅读您的文件(pandas 1.2.4):
import pandas as pd
pd.read_csv("sampledata.csv", escapechar="\\").T
Output: Output:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.