pandas Read_CSV报价问题 [英] Pandas Read_CSV quotes issue
本文介绍了 pandas Read_CSV报价问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的文件:
I have a file that looks like:
'colA'|'colB'
'word"A'|'A'
'word'B'|'B'
我想使用pd.read_csv('input.csv',sep='|', quotechar="'"
),但得到以下输出:
I want to use pd.read_csv('input.csv',sep='|', quotechar="'"
) but I get the following output:
colA colB
word"A A
wordB' B
最后一行不正确,应该为word'B B
.我该如何解决?我尝试了各种迭代,但是没有一个单词能正确读取两行.我需要一些csv阅读方面的专业知识!
The last row is not correct, it should be word'B B
. How do I get around this? I have tried various iterations but none of them word that reads both rows correctly. I need some csv reading expertise!
推荐答案
我认为您需要 apply
:
I think you need str.strip
with apply
:
import pandas as pd
import io
temp=u"""'colA'|'colB'
'word"A'|'A'
'word'B'|'B'"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), sep='|')
df = df.apply(lambda x: x.str.strip("'"))
df.columns = df.columns.str.strip("'")
print (df)
colA colB
0 word"A A
1 word'B B
这篇关于 pandas Read_CSV报价问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文