當我在一個txt文件這樣寫着:打開項目從一個字符串列表中
with open("sample.txt") as f:
content = f.readlines()
for row in content:
print(row)
我有以下輸入:
['k'], ['m']
我想變成這樣:
[k, m]
因此,因此我嘗試
with open("sample.txt") as f:
content = f.readlines()
for row in content:
items = items.replace('\'', '') for item in items
row_analyze = ','.join(items)
然而,這給了我:
[,,k,,],,,[,,m,,],
任何思考我做錯了什麼在這裏?
什麼數據真的包含在文件中? –
'items = items.replace('\'','')for item in items'沒有意義。你的意思是'items = [item.replace('\'','')for item in row]'? –
回過頭來,''sample.txt''從哪裏來?也許你可以通過'pickle'或者'json'模塊來避免這個中間文件或者把它作爲python對象正確保存 –