2017-08-28 200 views
-1

潑我從一個文本文件中的長字符串,我想拆python3字符串分隔符

數據=「什麼| BER |不確定|時間戳| 27-8-2017 22:54:54 |影片名稱|小姐|」

這裏是我讀取文件的部分,但我沒有看到我做錯了什麼打印數據(或在列表中)。

with open (fname, "r") as myfile: 
    data=myfile.readlines() 
    print (data.split('|')) 
    if (check in data): 

打印(data.split( '|')) AttributeError的: '名單' 對象有沒有屬性 '分裂'

人看到我在做什麼錯在這裏?

回答

4

您一次將所有行讀入數據變量,因此它包含一個列表。嘗試[0],而不是如下分割數據:

with open (fname, "r") as myfile: 
    data=myfile.readlines() 
    print (data[0].split('|')) 

或只讀一行如下:

with open (fname, "r") as myfile: 
    data=myfile.read() 
    print (data.split('|'))