我会尽量保持具体。请记住,我上周刚开始学习这门语言,所以我不是专业人士。
我正在尝试制作一个程序,该程序将读取我创建的词汇表文件,并将该词的定义写入另一个具有不同格式的预先存在的文件。两种格式的
例子,什么我想在这里做:
字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()
你有一些入门代码吗?编辑 – Seekheart
以显示代码。 – purplecracka