我有一个txt文件,它基本上是一个试卷,我想阅读该文件并在两个单词之间执行一些操作。
假设有3个部分,A部分,B部分,C部分,我想做一些操作,例如检查所有问题编号,在A部分和B部分之间是否存在。
的此链接我想检查A部分中是否存在所有12个问题
总而言之,我只想阅读txt文件中的两个词之间
这是完整的代码。 我在两个试卷之间进行比较 注意:其原始代码尚未完成
# Ask the user to enter the names of files to compare
fname1 = input("Enter the Blue print name: ")
fname2 = input("Enter the Source name: ")
# Open file for reading in text mode (default mode)
f1 = open(fname1)
f2 = open(fname2)
# Print confirmation
print("-----------------------------------")
print("Comparing files ", " BP " + fname1, " SRC " +fname2, sep='\n')
print("-----------------------------------")
# Read the first line from the files
f1_line = f1.readline()
f2_line = f2.readline()
# Initialize counter for line number
line_no = 1
NoError=True
# Loop if either file1 or file2 has not reached EOF
while f1_line != '' or f2_line != '':
# Strip the leading whitespaces
f1_line = f1_line.rstrip()
f2_line = f2_line.rstrip()
# Compare the lines from both file
if f1_line != f2_line:
# If a line does not exist on file2 then mark the output with + sign
if f2_line == '' and f1_line != '':
NoError=False
print("Additional line found in BP", "Line-%d" % line_no, f1_line)
# otherwise output the line on file1 and mark it with > sign
elif f1_line != '':
print("Please check BP", "Line-%d" % line_no, f1_line)
NoError=False
# If a line does not exist on file1 then mark the output with + sign
if f1_line == '' and f2_line != '':
print("Additional line found in SRC ", "Line-%d" % line_no, f2_line)
NoError=False
# otherwise output the line on file2 and mark it with < sign
elif f2_line != '':
print("Please check SRC", "Line-%d" % line_no, f2_line)
NoError=False
#Read the next line from the file
f1_line = f1.readline()
f2_line = f2.readline()
#Increment line counter
line_no += 1
if NoError:
print("Both files are same")
print("---------------------------------------------------------------------\n")
print("---------------------------------------------------------------------")
print("Checking for parts\n")
print("-----------------------------------")
#Checking for parts
#Put the parts in as p1 for part A, p2 for part b etc.....
p1 = "Part A"
p2 = "Part B"
#Checking parts condition in Blue print file
if p1 in open(fname1).read():
print("Part A found in BP")
else:
print("Part A not found BP")
if p2 in open(fname1).read():
print("Part B found in BP")
else:
print("Part B not found BP")
#Checking parts condition in Source file
if p1 in open(fname2).read():
print("Part A found in Src")
else:
print("Part A not found Src")
if p2 in open(fname2).read():
print("Part B found in Src")
else:
print("Part B not found Src")
print("---------------------------------------------------------------------\n")
print("---------------------------------------------------------------------")
print("Checking for questions\n")
print("---------BLUE PRINT---------")
print("PART A")
parta = ["{}.".format(i+1) for i in range(12)]
found_alla = True
with open(fname1) as file:
text = file.read()
for word in parta:
if word not in text:
found_alla= False
print("{} not found\n".format(word))
if found_alla:
print("all questions found in Part A of Blue print\n")
print("PART B")
partb = ["{}".format(i+1) for i in range(5)]
found_allb = True
with open(fname1) as file:
text = file.read()
for word in partb:
if word not in text:
found_allb= False
print("{} not found\n".format(word))
if found_allb:
print("all questions found in Part B of Blue print")
print("---------SOURCE---------")
print("PART A")
partas = ["{}.".format(i+1) for i in range(12)]
found_allaS = True
with open(fname2) as file:
text = file.read()
for word in partas:
if word not in text:
found_allaS= False
print("{} not found\n".format(word))
if found_allaS:
print("all questions found in Part A of SOURCE\n")
print("PART B")
partbS = ["{}.".format(i+1) for i in range(5)]
found_allBS = True
with open(fname2) as file:
text = file.read()
for word in partbS:
if word not in text:
found_allbS= False
print("{} not found\n".format(word))
if found_allbS:
print("all questions found in Part B of SOURCE")
f1.close()
f2.close()
input("Press Enter to close")
答案 0 :(得分:0)
您可以首先使用一些if语句搜索单词出现在哪一行。
然后在两行之间循环并按照您的意愿进行操作。
例如,如果第1部分在第10行上出现,而第2部分在第18行上出现,则在第10和第18行之间循环并按您的意愿进行操作。
答案 1 :(得分:0)
我可能会简化这个过程,但是您能不能只拆分标题以获得所需的内容?
text = 'PART A: This is Part A, ham and eggs. PART B: This is part B, eggs and ham'
text.split('PART A:')[1].split('PART B:')[0].strip()
结果:
'This is Part A, ham and eggs.'