
我的資料檔案包含一些無法從鍵盤定義以設定為分隔符的字符。無論如何以正確的方式讀取這些資料。
我的資料在 .txt 檔案中看起來不同,但當我粘貼在這里時,它看起來像:
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
PW 100 2000 2000 C 0 0 0 0.00 0.00 0
但我也在這里附上了原始資料data。
為了讀取資料,我只是通過這種方式嘗試過:
import pandas as pd
pd.read_table('data.txt',sep = '\s ')
有沒有更好的方法來做到這一點?
uj5u.com熱心網友回復:
您必須從不可見字符中洗掉檔案:
import pandas as pd
import io
import re
with open('pwd_data.txt') as fp:
buffer = io.StringIO(re.sub('[\01-\03]', '', fp.read()))
df = pd.read_table(buffer, header=None, sep='\s ')
輸出:
>>> df
0 1 2 3 4 5 6 7 8 9 10
0 PW 100 2000 2000 C 0 0 0 0.0 0.0 0
1 PW 100 2000 2000 C 0 0 0 0.0 0.0 0
2 PW 100 2000 2000 C 0 0 0 0.0 0.0 0
...
32 PW 100 2000 2000 C 0 0 0 0.0 0.0 0
33 PW 100 2000 2000 C 0 0 0 0.0 0.0 0
34 PW 100 2000 2000 C 0 0 0 0.0 0.0 0
>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 35 entries, 0 to 34
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 0 35 non-null object
1 1 35 non-null int64
2 2 35 non-null int64
3 3 35 non-null int64
4 4 35 non-null object
5 5 35 non-null int64
6 6 35 non-null int64
7 7 35 non-null int64
8 8 35 non-null float64
9 9 35 non-null float64
10 10 35 non-null int64
dtypes: float64(2), int64(7), object(2)
memory usage: 3.1 KB
uj5u.com熱心網友回復:
嘗試將您的 sep 更改為 r'[\s \x00-\x19]'
pd.read_table('data.txt',sep = r'[\s \x00-\x19]')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/383034.html
上一篇:我有一個要過濾的資料框
