简体   繁体   English

从 a.txt 文件中提取信息并转换为 int

[英]Extracting Info From a .txt File and Converting to int

I'm essentially trying to write a code that divides my tips each night into seperate savings plans and writes that to a.txt file.我实际上是在尝试编写一个代码,将我每晚的小费分成单独的储蓄计划,并将其写入 a.txt 文件。

I'm having issues reading the old total from the text file and adding the supplimentary tips then rewriting the product to the.txt file.我在从文本文件中读取旧总数并添加补充提示然后将产品重写到 .txt 文件时遇到问题。

Please tell me if this is a ridiculous way of doing things and I need to change my whole way of doing things.请告诉我这是否是一种荒谬的做事方式,我需要改变我的整个做事方式。

import re


savings1 = 0  
savings2 = 0
savings3 = 0
savings4 = 0
savings5 = 0
savings6 = 0

'initint' takes in the value. 'initint' 接受该值。

initint = input('Please input todays tips, or Enter to exit:')

Then the while loop divides the value into respective percentages and allocates them to my savings counters.然后 while 循环将值划分为各个百分比并将它们分配给我的储蓄计数器。

while initint != "":
    
    savings1 += (int(initint)*0.50)
    savings2 += (int(initint)*0.05)
    savings3 += (int(initint)*0.25)
    savings4 += (int(initint)*0.10)
    savings5 += (int(initint)*0.05)
    savings6 += (int(initint)*0.05)
    
    initint = input('Please input todays tips, or Enter to exit:')
    
    if initint == "":
        break

This is where (I think) the trouble comes in.这就是(我认为)问题所在。

The idea is to use regex to identify numbers in the txt file then return them so I can add them to the value that the while loop divided up.这个想法是使用正则表达式来识别 txt 文件中的数字,然后返回它们,这样我就可以将它们添加到 while 循环划分的值中。

At the moment I can only print them.目前我只能打印它们。

with open('divisions.txt','r+') as r:
    for line in r:
        
        I = re.findall('\d+\d', line)
        
        print(I)

So the output is looking like this at the moment (if the old input was 500).所以 output 现在看起来像这样(如果旧输入是 500)。

[]
[]
['250']
['25']
['125']
['50']
['25']
['25']

The first issue is that it's returning lists.第一个问题是它正在返回列表。

The second issue is that it's returning 2 empty lists from the first 2 lines with no digits which is a bit strange.第二个问题是它从前 2 行返回 2 个没有数字的空列表,这有点奇怪。

I have no idea what I would even google to start this step.我不知道我什至会用谷歌来开始这一步。


The last step writes the totals to the text file.最后一步将总数写入文本文件。


with open('divisions.txt','w') as f:
    
    f.write(f'Savings Plan:\n\nsavings1: {savings1}\nsavings2: {savings2}\nsavings3: {savings3}\nsavings4: {savings4}\nsavings5: {savings5}\nsavings6: {savings6}\n')

Let me know if I'm being silly but would very much appreciate it if anyone is willing to help.如果我很傻,请告诉我,但如果有人愿意提供帮助,我将不胜感激。

Thanks for reading.谢谢阅读。

Your regex assumes that all numbers are double digits or more, I would change this to r'\d+' .您的正则表达式假定所有数字都是两位数或更多,我会将其更改为r'\d+'

The findall function always returns a list, but you can check if the length is more than 0 and grab the first number. findall function 总是返回一个列表,但是你可以检查长度是否大于0,并获取第一个数字。

with open('divisions.txt','r+') as r:
    for line in r:
    
        I = re.findall(r'\d+', line)
        if (len(I) > 0):
            number = I[0]
            print(number)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM