![](/img/trans.png)
[英]pandas read_csv index_col=None not working with delimiters at the end of each line
[英]Ignore delimiters at end of row in Pandas read csv
我在 CSV 文件中有數據。 我使用單個制表符將數據分成列。 大多數行只包含一個制表符,如下所示:
A\tB
有些行在行尾包含額外的選項卡,如下所示:
A\tB\t\t
因此,如果我執行pd.read_csv(filePath, sep='\t')
,則會收到錯誤消息: ParserError: Error tokenizing data. c error: Expected 2 fields in line XXX, saw 4
ParserError: Error tokenizing data. c error: Expected 2 fields in line XXX, saw 4
。 這是因為有些行包含 4 個選項卡。
那么,如果一行末尾包含額外的標簽,我怎么能忽略它呢?
之前使用io.StringIO
清理文件:
import pandas as pd
import io
with open('data.txt') as table:
buffer = io.StringIO('\n'.join(line.strip() for line in table))
df = pd.read_table(buffer, header=None)
Output:
>>> df
0 1
0 A B
1 A B
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.