2016-02-28 92 views
0

我會盡量保持具體。請記住,我上週剛開始學習這門語言,所以我不是專業人士。

我正在嘗試製作一個程序,該程序將讀取我創建的詞彙表文件,並將該詞的定義寫入另一個具有不同格式的預先存在的文件。兩種格式的
例子,什麼我想在這裏做:
       字1 - 定義
       字1(頁531) - 從其他文件

我的定義是什麼米目前正在做的是我打開這兩個文件,並根據用戶輸入搜索一個詞,這是行不通的。我想要做的是我想要程序進入輸出文件並找到該單詞,然後在輸入文件中找到相同的單詞,只獲取定義並將其粘貼到輸出文件中。然後移動到下一個單詞並循環,直到找到文件的結尾。我真的不知道該怎麼做,所以我目前被卡住了。你將如何在Python的優點這裏在stackoverflow處理這個?

同樣對於那些對我的計劃產生懷疑的人,我並沒有試圖欺騙一項任務,我試圖提前完成我的一些大學工作,而且我不想跑與我的格式不一致的教師衝突。這只是爲了節省我的時間,所以我不必兩次做同樣的任務。如何動態搜索文件中的文本並寫入另一個文件

編輯1 這裏是從我的程序目前粘貼的完整代碼。

import os 

print("Welcome to the Key Terms Finder Program. What class is this for?\n[A]ccess\n[V]isual Basic") 
class_input = raw_input(">>") 
if class_input == "A" or class_input == "a": 
    class_input = "Access" 
    chapter_num = 11 
elif class_input == "V" or class_input == "v": 
    class_input = "Visual Basic" 
    chapter_num = 13 
else: 
    print("Incorrect Input") 
print("So the class is " + class_input) 
i = 1 
for i in range(1, chapter_num + 1): 
    try: 
     os.makedirs("../Key Terms/" + class_input + "/Chapter " + str(i) + "/") 
    except WindowsError: 
     pass 
print("What Chapter is this for? Enter just the Chapter number. Ex: 5") 
chapter_input = raw_input(">>") 
ChapterFolder = "../Key Terms/" + class_input + "/Chapter " + str(chapter_input) + "/" 
inputFile = open(ChapterFolder + "input.txt", "r") 
outputFile = open(ChapterFolder + "output.txt", "w") 
line = inputFile.readlines() 
i = 0 
print("Let's get down to business. Enter the word you are looking to add to the file.") 
print("To stop entering words, enter QWERTY") 
word_input = "" 
while word_input != "QWERTY": 
    word_input = raw_input(">>") 
    outputArea = word_input 
    linelen = len(line) 
    while i < linelen: 
     if line[i] == word_input: 
      print("Word Found") 
      break 
     else: 
      i = i + 1 
     print(i) 
    i = 0 

inputFile.close() 
outputFile.close() 
+0

你有一些入門代碼嗎?編輯 – Seekheart

+0

以顯示代碼。 – purplecracka

回答

1

不是python pro,但是,我會嘗試回答你的問題。

output=[] 
word=[] 
definition=[] 
with open('input.txt','r') as f: 
     for line in f: 
       new_line=re.sub('\n','',line) 
       new_line=re.sub('\s+','',line) 
       word.append(new_line.split("-")[0]) 
       definition.append(new_line.split("-")[1]) 
with open('output.txt','r') as f: 
     for line in f: 
       new_line=re.sub('\n','',line) 
       new_line=re.sub('\s+','',line) 
       try: 
         index = word.index(new_line) 
         print index 
         meaning = definition[index] 
         print meaning 
         output.append(new_line+" - "+meaning) 
       except ValueError as e: 
         output.append(new_line+" - meaning not found") 
         print e 
f=open("output.txt","w") 
f.write("\n".join(output)) 
f.close() 

這裏,input.txt是單詞和定義存在的文件。 output.txt是隻有單詞的文件(我不清楚output.txt包含的是什麼,我只假設單詞)。 上面的代碼是從output.txt中讀取數據,查看input.txt並獲取定義,如果發現它跳過。

假設是單詞和定義是由 -

這是否有幫助嗎?

+0

它的第一部分大部分工作。我已經像輸入文件上的re.sub(「\ s +」,「」,line)一樣將它編輯爲re.sub(「\ s +」,「」,line),因爲由於某種原因它正在顯示從單詞1到單詞1的單詞,但這只是我調試,看看我能否全部弄清楚。我無法弄清楚的問題是,它不會像輸出文件中那樣讀取單詞。這確實有助於思考,謝謝。 – purplecracka

相關問題