,所以我有這樣的文字(共發現)文件由數字和文字,例如像這樣的 -使用字符串中的一個整數創建一個字典(或列表)與許多數字
"09807754 18 n 03 aristocrat 0 blue_blood 0 patrician"
和我想在第一個數字中作爲字典名稱(或列表)讀取以下單詞。這個佈局永遠不會改變,它始終是一個8位數字鍵,後面跟着一個兩位數字,一個字母和一個兩位數字。最後兩位數字(03)表示在第一個8位數字鍵上有多少個單詞(在這種情況下是三個單詞)。
我的想法是,我將搜索字符串中的第14位,並使用該號碼來運行一個循環,所有與該密鑰
,所以我認爲它會去這樣的事情相關的話來接
with open('nouns.txt','r') as f:
for line in f:
words = range(14,15)
numOfWords = int(words)
while i =< numOfWords
#here is where the problem arises,
#i want to search for words after the spaces 3 (numOfWords) times
#and put them into a dictionary(or list) associated with the key
range(0,7) = {word(i+1), word(i+2)}
技術上我找取其中的一個更有意義:
09807754 = { 'word1':aristocrat, 'word2':blue_blood , 'word3':patrician }
or
09807754 = ['aristocrat', 'blue_blood', 'patrician']
顯然,這並不運行,但如果任何人都可以給我任何指針也將不勝感激
優秀!還有一件事,如果整個文本行是這樣的話,有沒有辦法阻止它最後一句話: 09827177 18 n 03貴族0 blue_blood 0貴族0 013 @ 09646208 n 0000 – Johnnerz
@Johnnerz,當然我把它添加到我的回答 –
嘿,還有一件小事情,在這些行中,我想添加任何東西后|並將其作爲該密鑰的另一個條目,我該怎麼做? '09826918 18 n 01 Argive 0 002 @ 09729560 n 0000 + 08804512 n 0101 |阿爾戈斯市的本地居民或居民 – Johnnerz