我需要通過,看起來像一個文本文件來解析解析:使用Python通過文本文件分隔符
"id"$"date"$"text"
10001$2016-01-11$"[start]
this is some text
[stop]
"
10002$2014-03-12$"[start]
this is some more text
[stop]
"
與Python到庫中,並有這三個不同的元素(ID,日期和文字)作爲鑰匙。
我不知道如何使用分隔符分割這些元素,以及如何將第一行用作列表中所有元素的鍵。
能像這樣工作,甚至只打印:
infile = open('filename.txt', 'r')
for line in infile:
if "????" in line:
print(line, next(infile))
如果我嘗試:
infile = open('filename.txt', 'r')
for line in infile:
if '"text"' in line:
print(next(infile))
只打印第一行。
這將理想的樣子:
[{'id':'10001', 'date':'2016-01-11', 'text':'this is some text'},{'id':'10002', 'date':'2014-03-12', 'text':'this is some more text'}]
什麼樣的麻煩?向我們展示您使用的代碼。 –
我認爲如果你發佈文件本身就是最好的。 –
Python'list'沒有命名的「keys」。作爲Python類型,結果數據結構的期望格式是什麼? – Kupiakos