从文本中提取数字



我有一个文本文件,其结果如下所示:

欢迎使用您的结果

50+25=75 的结果

60+25=85 的结果

70+25=95 的结果

80+25=105 的结果

我需要python来读取文件,并将数字拉到"="并将它们相加。我不知道该怎么做。

# take input from the user
print("Select operation.")
print("1.Display The Content of the result file")
print("2.Add two numbers")
print("3.Subtract two numbers")
print("4.Multiply two numbers")
print("5.Divide two numbers")
print("6.Append results to file")
print("7.Display Total of inputs")
print("8.Display Average of inputs")
print("9.Exit")
choice = input("Select an option(1-9):")
if choice == 1:
    file = open('results.txt', 'r')
    print file.read()
    
elif choice >= 2 and choice <= 5:
    l_range = int(input("Enter your Lower range: "))
    h_range = int(input("Enter your Higher range: "))
    num1 = int(input("Enter first number: "))
    num2 = int(input("Enter second number: "))
    
    if num1 < l_range:
        print "The input values are out side the input ranges n Please check the numbers and try again" 
    elif num2 > h_range:
        print "The input values are out side the input ranges n Please check the numbers and try again"
    else:
        if choice == 2:
            print "The result of", num1,"+", num2,"=", add(num1,num2)
            resultstr = "The result of " +  str(num1)  + "+" +  str(num2) + "=" +  str(add(num1,num2))
                        
        elif choice == 3:
            print "The result of", num1,"-",num2,"=", subtract(num1,num2)
            resultstr = "The result of "  +  str(num1) + "-" + str(num2) + "=" + str(subtract(num1,num2))
            
        elif choice == 4:
            print "The result of", num1,"*", num2,"=", multiply(num1,num2)
            resultstr = "The result of "  +  str(num1) + "*" + str(num2) + "=" + str(multiply(num1,num2))
        elif choice == 5:
            print "The result of", num1,"/", num2, "=", divide(num1,num2) 
            resultstr = "The result of "  +  str(num1) + "/" + str(num2) + "=" + str(divide(num1,num2))
            if num2 == 0:
                print "The result of", num1,"/", num2, "=", "You can't divide by zero!"
                                                    
elif choice == 6:
    print("The results have been appended to the file")
    file = open('results.txt', 'a')
                 
    file.write(resultstr + "n")
                             
    file.close()
           
elif choice == 7:
    print ("Display total inputs")
elif choice == 8:
    print ("Display average of total inputs")
elif choice == 9:
    print ("Goodbye!")
else:
    print("Invalid input")   
lp_input = raw_input("Do you want to perform another action? Y/N:")
    
if lp_input == 'n':
    print("Goodbye!")

根据我对这个问题的理解,一个简单的正则表达式匹配就能解决问题。你可以这样做:

逐行读取文件。制作这样的regex模式,从字符串中获取数据:

>>> pattern = "The result of (d+?)[+-/*](d+?)=(d+).*"

假设文件中的一行存储在变量结果中:

>>> result = "The result of 25+50=75"

导入re库并使用类似的匹配函数,该函数为您提供字符串中的数据:

>>> s = re.match(pattern, result)

>>> print s.groups()

('25', '50', '75')

然后,您可以从这个元组中获取数字并对其进行任何操作。更改正则表达式以满足您的需要,但考虑到文件开头的内容,这应该没问题。

相关内容

  • 没有找到相关文章

最新更新