简体   繁体   English

如何从txt文件中提取数字

[英]How to extract number from a txt file

First my file首先是我的档案

amtdec = open("amt.txt", "r+")
gc = open("gamecurrency.txt", "r+")

eg = gc.readline()
u = amtdec.readline()

The main code主要代码

user_balance = int(u)
egc = int(eg)

while True:
    deposit_amount = int(input("Enter deposit amount: $"))
    if deposit_amount<=user_balance:
            entamount = deposit_amount * EXCHANGE_RATE
            newgc = entamount + egc
            newamt = user_balance - deposit_amount

This is what my error was:这就是我的错误:

 user_balance = int(u)
ValueError: invalid literal for int() with base 10: ''

I was trying to compare a int in a file with my input.我试图将文件中的 int 与我的输入进行比较。

Usually an error like this should turn you to check the formatting of your file.通常这样的错误会让您检查文件的格式。 As some others mentioned, the first line could be empty for whatever reason.正如其他一些人提到的,无论出于何种原因,第一行都可能是空的。 You can check for an empty file prior to this by doing the following:您可以通过执行以下操作在此之前检查空文件:

test.txt contents: (empty file) test.txt 内容:(空文件)

import os

f = open("test.txt")

if os.path.getsize("test.txt") == 0:
    print("Empty File")
    f.close()
else:
    print("Some content exists")

Output: "Empty File" (file is closed too since there is nothing to read) Output:“空文件”(文件也已关闭,因为没有可读的内容)

Alternatively, you can read the entire file if you somehow can't access its contents (some schools do this).或者,如果您无法访问其内容,您可以阅读整个文件(有些学校这样做)。 Using this technique will give you an idea of what you are dealing with in your file if you can't view it within your IDE:如果您无法在 IDE 中查看文件,使用此技术将使您了解文件中正在处理的内容:

f = open("test.txt")

for line in f:
    print(line)

f.close()

But let's say that just the first line of your file is empty.但是假设文件的第一行是空的。 There are several ways you can check if a line is empty.有几种方法可以检查一行是否为空。 If line 1 is blank but any line following it has content, reading line 1 from file will equal '\n':如果第 1 行为空但其后的任何行都有内容,则从文件中读取第 1 行将等于“\n”:

test.txt contents: line 1 = '\n' (blank line), line 2 = 20.72 test.txt 内容:第 1 行 = '\n'(空行),第 2 行 = 20.72

import os

f = open("test.txt")

if os.path.getsize("test.txt") == 0:
    print("Empty File")
    f.close()
else:
    print("Some content exists")

reader = f.readline()

# The second condition is if you are using binary mode
if reader == '\n' or reader == b"\r\n":
    print("Blank Line")

Output: "Some content exists" & "Blank line" Output:“一些内容存在”&“空白行”

This is just my suggestion.这只是我的建议。 As for your integer conversion, if you have a '.'至于你的 integer 转换,如果你有一个 '.' in your currency amount, you will get a conversion error for trying to data cast it into an integer. However, I do not know if your currency will be rounded off to the nearest dollar or if you have any indication of change, so I will leave this to you.在您的货币金额中,您会在尝试将其数据转换为 integer 时遇到转换错误。但是,我不知道您的货币是否会四舍五入到最接近的美元,或者您是否有任何变化的迹象,所以我会把这个留给你。

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

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