从tex中提取数字

2024-04-26 06:04:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Welcome to your results

The result of 50+25=75

The result of 60+25=85

The result of 70+25=95

The result of 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!")

Tags: oftheinputrangeresultresultsfileprint
1条回答
网友
1楼 · 发布于 2024-04-26 06:04:35

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

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

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

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

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

导入re库并像这样使用match函数,它将从字符串中获取数据:

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

>>> print s.groups()

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

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

相关问题 更多 >