2016-02-28 93 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

相关问题