0

我的目标是将文本文件中的行打印在一起。然而,有些行并没有像他们应该的那样在一起。我解决了分母在后面的第一个问题。对于该else声明,它们似乎都具有相同的值/索引。

import fitz  # this is pymupdf

with fitz.open("math-problems.pdf") as doc: #below converts pdf to txt
    text = ""
    for page in doc:
        text += page.getText()

file_w = open("output.txt", "w") #save as txt file
file_w.write(text)
file_w.close()

file_r = open("output.txt", "r") #read txt file
word = 'f(x) = '

#--------------------------
list1 = file_r.readlines()  # read each line and put into list

list2 = [k for k in list1 if word in k] # look for all elements with "f(x)" and put all in new list

list1_N = list1
list2_N = list2
list1 = [e[3:] for e in list1] #remove first three characters (the first three characters are always "1) " or "A) "

char = str('\n')

for char in list2:
    index = list1.index(char)
    def digitcheck(s):
        isdigit = str.isdigit
        return any(map(isdigit,s))
    xx = digitcheck(list1[index])
    if xx:
        print(list1[index] + " / " + list1_N[index+1])
    else:
        print(list1[index] + list1[index+1]) # PROBLEM IS HERE, HOW COME EACH VALUE IS SAME HERE?


终端输出:

f(x) = x3 + x2 - 20x
 / x2 - 3x - 18

f(x) = 
2 + 5x

f(x) = 
2 + 5x

f(x) = 
2 + 5x

f(x) = 
2 + 5x

f(x) = x2 + 3x - 10
 / x2 - 5x - 14

f(x) = x2 + 2x - 8
 / x2 - 3x - 10

f(x) = x - 1
 / x2 + 8

f(x) = 3x3 - 2x - 6
 / 8x3 - 7x + 4

f(x) = 
2 + 5x

f(x) = x3 - 6x2 + 4x - 1
 / x2 + 8x


Process finished with exit code 0
4

1 回答 1

0

已解决@copperfield 是正确的,我有重复的值,所以我的索引是重复的。我在这里使用@Shonu93 的解决方案解决了这个问题。本质上,它定位所有重复值的索引并将这些索引放入一个列表中elem_pos,然后从list1

if empty in list1:
counter = 0
elem_pos = []
for i in list1:
    if i == empty:
        elem_pos.append(counter)
    counter = counter + 1
xy = elem_pos

for i in xy:
print(list1[i] + list1_N[i+1])
于 2021-02-10T04:55:55.520 回答