Python导入txt格式

发布于 2024-10-17 13:03:30 字数 584 浏览 1 评论 0原文

我有一个包含数字列表的 Excel 文件,我将其保存为 .txt 文件,然后说:

open_file = open('list_of_numbers.txt','r')

for number in open_file:
    number = int(number)
    while x < 20000:
        if (x > number):
            print number
        x = x + 100
        y = y + 100

我收到了以下错误消息:

ValueError:以 10 为基数的 int() 的文字无效:'2100.00\r\n'

如何删除 '\r\n'

我的最终目标是在数字列旁边创建另一列,例如,如果数字是 145,则创建

145, '100-199'
167, '100-199'
1167, '1100-1199'

此类输出。

I have an Excel file with a list of numbers and I saved it as a .txt file and then went to say:

open_file = open('list_of_numbers.txt','r')

for number in open_file:
    number = int(number)
    while x < 20000:
        if (x > number):
            print number
        x = x + 100
        y = y + 100

And I received this error message:

ValueError: invalid literal for int() with base 10: '2100.00\r\n'

How can I strip the ' and the \r\n'?

My ultimate goal is to create another column next to the column of numbers and, if the number is 145 for example,

145, '100-199'
167, '100-199'
1167, '1100-1199'

that sort of output.

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

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

发布评论

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

评论(3

救赎№ 2024-10-24 13:03:30

让我们把它作为答案。问题不在于 \r\n。问题是您尝试将包含浮点值的字符串解析为整数。请参阅(无换行符,换行符):

>>> int("2100.00")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2100.00'

(如您所见,引号 ' 不是值的一部分,它们只是表明您正在处理字符串)< /em>

>>> int("2100\r\n")
2100

文档 说:

如果参数是字符串,它必须包含一个可能有符号的十进制数,可表示为 Python 整数,可能嵌入空格中。

Python 整数文字定义可以在 此处

解决方案:

使用float

>>> float("2100.00\r\n")
2100.0

如果需要,您可以将其转换为整数(也可以考虑 round):

>>> int(float("2100.00\r\n"))
2100

将浮点值转换为整数有效(来自 文档):

浮点数到整数的转换会截断(朝向零)。

Let's put it as an answer. The problem is not \r\n. The problem is that you try to parse string that contains a float value as an integer. See (no line feed, new line characters):

>>> int("2100.00")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2100.00'

(as you can see, the quotation marks ' are not part of the value, they just indicate that you are dealing with a string)

whereas

>>> int("2100\r\n")
2100

The documentation says:

If the argument is a string, it must contain a possibly signed decimal number representable as a Python integer, possibly embedded in whitespace.

where the Python integer literal definition can be found here.

Solution:

Use float:

>>> float("2100.00\r\n")
2100.0

then you can convert it to an integer if you want to (also consider round):

>>> int(float("2100.00\r\n"))
2100

Converting a float value to integer works (from the documentation):

Conversion of floating point numbers to integers truncates (towards zero).

堇色安年 2024-10-24 13:03:30

为了解决您眼前的问题,请参考@Felix Kling 的答案。

如果您对未来的问题感兴趣,请继续阅读。

(1) 在这种特殊情况下, \r 不是问题的一部分,但很有趣:您是否在 Windows 上创建文件并在 Linux/OSX/etc 上读取它?如果是这样,您应该使用“rU”(通用换行符)打开文本文件,以便 Python 上的输入行只有 \n

(2) 无论如何,最好执行 line = line.rstrip('\n') ...否则,根据您如何分割行,您最终可能会最后一个字段包含不需要的 \n

(3) 您可能更喜欢使用 xlrd 直接从 Excel 文件中读取 - 这可以节省各种麻烦麻烦。 [Dis]声明:我是 xlrd 的作者。

To address your immediate problem, go with the answer by @Felix Kling.

If you are interested in your FUTURE problems, please read on.

(1) That \r is not part of the problem IN THIS PARTICULAR CASE, but is intriguing: Are you creating the file on Windows and reading it on Linux/OSX/etc? If so, you should open your text file with "rU" (universal newlines), so that the input line on Python has only the \n.

(2) In any case, it's a very good idea to do line = line.rstrip('\n') ... otherwise, depending on how you split up the lines, you may end up with your last field containing an unwanted \n.

(3) You may prefer to use xlrd to read from an Excel file directly -- this saves all sorts of hassles. [Dis]claimer: I'm the author of xlrd.

薄情伤 2024-10-24 13:03:30

试试这个:

number = int(number.strip(string.whitespace + "'"))

您需要将 import string 添加到脚本的开头。另请参阅:http://docs.python.org/library/stdtypes.html#字符串条

Try this:

number = int(number.strip(string.whitespace + "'"))

You will need to add import string to the beginning of the your script. See also: http://docs.python.org/library/stdtypes.html#str.strip

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文