如何使用 Python 从文本文件中获取整数?

发布于 2024-12-18 02:44:07 字数 849 浏览 0 评论 0原文

在Python中,我试图从存储在txt中的信息设置一个变量。我可以设法设置它,但它不能充当数字。它是否被存储为其他东西? 这是我的 .txt 文件(所有信息都写在一行上,没有 \ns):

11 14 15 3

这是我的脚本:

def break_words(text):
    words = text.split(' ')
    return words

file_name = open("set_initial.txt")
words = break_words(file_name.read())

shift_l1 = words[0]
shift_l2 = words[1]
shift_l3 = words[2]
shift_l4 = words[3]
shift_l5 = words[4]
shift_l6 = words[5]

# this part is to verify that the variables are being set:
print shift_l1, shift_l2, shift_l3, shift_l4

while shift_l4 < 28:
# and the script goes on into a loop from here

我使用这种方法是因为 txt 中的值的长度会发生变化(例如变为: 114 34 2 4318 )。当我运行脚本时,打印函数工作正常,并将变量返回为 .txt 中的数字(分别为 11 14 15 3),因此 shift_l4 打印为 3,因此我的 WHILE 循环应该正常运行。但事实并非如此。正如我所说,我认为我的变量没有被设置为 .txt 中数字的数值,但也许只是文本值?但我不知道如何解决它。有什么帮助或想法吗?

谢谢

in Python I'm trying to set a variable from info stored in a txt. I can manage to set it, but it does not act as a number. Is it being stored as something else?
Here is my .txt file (all info is written on one line, no \ns):

11 14 15 3

Here is my script:

def break_words(text):
    words = text.split(' ')
    return words

file_name = open("set_initial.txt")
words = break_words(file_name.read())

shift_l1 = words[0]
shift_l2 = words[1]
shift_l3 = words[2]
shift_l4 = words[3]
shift_l5 = words[4]
shift_l6 = words[5]

# this part is to verify that the variables are being set:
print shift_l1, shift_l2, shift_l3, shift_l4

while shift_l4 < 28:
# and the script goes on into a loop from here

I am using this method because the length of the values in the txt will change (for example to: 114 34 2 4318). When I run the script, the print function works fine and returns my variables as the numbers I have in my .txt (11 14 15 3 respectively), so shift_l4 prints out as 3, so my WHILE loop should be functioning. But it's not. As I said, I figure my variables aren't being set to the number value of the numbers in the .txt, but maybe just the text value? I don't know how to fix it though. Any help or ideas?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面犯桃花 2024-12-25 02:44:07

我认为问题在于比较字符串和整数中有两种不同的类型。假设您正在进行数值比较,您可能需要显式地将 shift_l4 转换为 int。 int(shift_l4)。

I think the problem is that you have two different types in your comparison string and int. Assuming you are doing a numerical comparison you may want to explicitly cast your shift_l4 to an int. int(shift_l4).

无声无音无过去 2024-12-25 02:44:07

“file_name”对于文件对象来说不是一个好的名称。对于表示为字符串的数字集合来说,“words”并不是一个好名字。 “shift_l5=words[4]”和“shift_l6=words[5]”将会失败,因为你只有 4 个数字。

请注意,print "3"print 3 产生相同的结果。使用 print repr(something) 而不是仅仅 print Something 来获取您实际拥有的数据的句柄。

试试这个:

f = open("set_initial.txt")
numbers = [int(n) for n in f.read().split()]
print numbers
assert len(numbers) == 4
shift_l1, shift_l2, shift_l3, shift_l4 = numbers
print shift_l1, shift_l2, shift_l3, shift_l4

"file_name" is not a good name for a file object. "words" is not a good name for a collection of numbers represented as strings. "shift_l5 = words[4]" and "shift_l6 = words[5]" will fail, because you have only 4 numbers.

Note that print "3" and print 3 produce the same results. Use print repr(something) instead of just print something to get a handle on what data you actually have.

Try this:

f = open("set_initial.txt")
numbers = [int(n) for n in f.read().split()]
print numbers
assert len(numbers) == 4
shift_l1, shift_l2, shift_l3, shift_l4 = numbers
print shift_l1, shift_l2, shift_l3, shift_l4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文