Python 从文本中提取数字

Python 从文本中提取数字,python,file,text,calculator,Python,File,Text,Calculator,我有一个文本文件,其结果如下所示: 欢迎查看您的结果 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

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

欢迎查看您的结果

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!")

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

逐行读取文件。 创建这样的正则表达式模式,以从字符串中获取数据:

>>>模式=\d+?[+-/*]\d+?=\d+.*的结果

假设变量result中存储了文件中的一行:

>>>结果=25+50的结果=75

导入re库并使用match函数(如所示),该函数提供字符串中的数据:

>>>s=重新匹配模式、结果

>>>打印s组

‘25’、‘50’、‘75’


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

它们都是加法和正数吗?我投票结束这个问题,因为这既不是代码编写也不是教程服务这是一项简单的任务。你尝试过什么?我意识到这可能是一项简单的任务,但不一定要拆分字符串然后使用它们。@jornsharpe如果你愿意,我们可以关闭它。我在多个vba论坛上,在注册的基础上帮助新的vba用户。我已经写了90%的申请,但不知道从哪里开始这部分。我已经阅读了多篇关于从文件中读取/拆分文本的教程,但不知道如何组合该过程。@kunalaggrarwal我如何让它只获得与模式匹配的行?file=open'results.txt',r'file。每行'results.txt'do | li |如果li[结果是\d+?[-/*]\d+?=\d+.] end@ScottMorins=re.matchpattern,如果行不匹配,结果将在s中为您提供无。您可以在if语句(如if s:)中检查,只有当行匹配时,这才是真的。