У меня есть текстовый файл, который в основном представляет собой вопросник, я хотел прочитать этот файл и выполнить некоторые операции между двумя словами.
Допустим, есть 3 части, ЧАСТЬ A, ЧАСТЬ B, ЧАСТЬ C, и я хотел сделать некоторые операции, такие как проверка всех номеров вопросов, если они присутствуют между ЧАСТЬЮ А и ЧАСТЬЮ Б *.. 1003 *
Проверьте эту ссылку вопросника
Я хотел проверить, присутствуют ли все 12 вопросов в разделе A
. Подводя итог, я хотел прочитать только два слова в текстовом файле
Это весь код.Я сравниваю два вопроса. Примечание: его необработанный код не завершен
# 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")