你的文件是一個固定寬度的文件,以便您可以使用read_fwf
,此處默認參數能夠推斷出列寬:
In [106]:
df = pd.read_fwf(r'icd10cm_codes_2017.txt', header=None)
df.head()
Out[106]:
0 1
0 A000 Cholera due to Vibrio cholerae 01, biovar chol...
1 A001 Cholera due to Vibrio cholerae 01, biovar eltor
2 A009 Cholera, unspecified
3 A0100 Typhoid fever, unspecified
4 A0101 Typhoid meningitis
如果你知道你想要的名稱的列名,你可以通過這些來read_fwf
:
In [107]:
df = pd.read_fwf(r'C:\Users\alanwo\Downloads\icd10cm_codes_2017.txt', header=None, names=['col1', 'col2'])
df.head()
Out[107]:
col1 col2
0 A000 Cholera due to Vibrio cholerae 01, biovar chol...
1 A001 Cholera due to Vibrio cholerae 01, biovar eltor
2 A009 Cholera, unspecified
3 A0100 Typhoid fever, unspecified
4 A0101 Typhoid meningitis
或者只是簡單地覆蓋閱讀後columns
屬性:
df.columns = ['col1', 'col2']
至於爲什麼你嘗試失敗,read_table
使用製表符作爲默認分隔符,但文件只是有空格並且寬度固定
請問您能解釋'sep = r'\ s {2,}',engine ='python''參數嗎?我從來沒有用'r'\ s {2,}'作爲分隔符,或者在engine ='python''中聲明瞭一個'engine'。 –
無法獲得第一個選項,但使用read_fwf()標題和名稱。現在正在工作。 read_fwf()對我來說是全新的。需要閱讀它。 –
感謝您解釋sep = r'\ s {2,}'。真的有用! –