Python导入txt格式
我有一个包含数字列表的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们把它作为答案。问题不在于
\r\n
。问题是您尝试将包含浮点值的字符串解析为整数。请参阅(无换行符,换行符):(如您所见,引号
'
不是值的一部分,它们只是表明您正在处理字符串)< /em>而
文档 说:
Python 整数文字定义可以在 此处。
解决方案:
使用
float
:如果需要,您可以将其转换为整数(也可以考虑
round
):将浮点值转换为整数有效(来自 文档):
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):(as you can see, the quotation marks
'
are not part of the value, they just indicate that you are dealing with a string)whereas
The documentation says:
where the Python integer literal definition can be found here.
Solution:
Use
float
:then you can convert it to an integer if you want to (also consider
round
):Converting a float value to integer works (from the documentation):
为了解决您眼前的问题,请参考@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.
试试这个:
您需要将
import string
添加到脚本的开头。另请参阅:http://docs.python.org/library/stdtypes.html#字符串条Try this:
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