Python 带 \n 的条带
这是我的问题。
我正在尝试读取文本文件,然后将行转换为浮点数。文本文件中有 \n
和 \t
虽然我不知道如何摆脱它。
我尝试使用 line.strip() ,但它没有将其取消,并且当我想将这些内容转换为浮点数时出现错误。然后我尝试了 line.strip("\n")
但这也不起作用。当我从文本文件中取出 \t
和 \n
时,我的程序工作正常,但这是让它与它们一起工作的任务的一部分。
我真的不知道为什么这不起作用。感谢您的任何帮助。
This is my problem.
I'm trying to read a text file and then convert the lines into floats. The text file has \n
and \t
in it though I don't know how to get rid of it.
I tried using line.strip()
but it didn't take it off and I got an error when I wanted to convert the stuff to floats. I then tried line.strip("\n")
but that didn't work either. My program works fine when I take out the \t
and \n
from the text file, but it's part of the assignment to get it to work with them.
I really don't know why this isn't working. Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您应该能够使用
line.strip('\n')
和line.strip('\t')
。但这些不会修改line
变量...它们只是返回删除了\n
和\t
的字符串。所以你必须做类似的事情,这应该适用于从开始和结束处删除。如果字符串中间有
\n
和\t
,则需要替换
\n
和\ t
虚无。You should be able to use
line.strip('\n')
andline.strip('\t')
. But these don't modify theline
variable...they just return the string with the\n
and\t
stripped. So you'll have to do something likeThat should work for removing from the start and end. If you have
\n
and\t
in the middle of the string, you need to doto replace the
\n
and\t
with nothingness.strip()
方法默认删除空格,因此无需使用 '\t' 或 '\n' 等参数调用它。然而,Python 中的字符串是不可变的并且无法修改,即line.strip()
调用不会更改line
对象。结果是调用返回的新字符串。正如已经提到的,如果您从输入文件中发布一个示例,将会有所帮助。如果每一行有多个数字,则不能使用
strip()
函数。相反,您应该使用 split(),它也是一个字符串方法。总之,假设每行包含多个由空格分隔的浮点数,并且您想要构建所有数字的列表,您可以尝试以下操作:
The
strip()
method removes whitespace by default, so there is no need to call it with parameters like '\t' or '\n'. However, strings in Python are immutable and can't be modified, i.e. theline.strip()
call will not change theline
object. The result is a new string which is returned by the call.As already mentioned, it would help if you posted an example from your input file. If there are more than one number on each line,
strip()
is not the function to use. Instead you should usesplit()
, which is also a string method.To conclude, assuming that each line contains several floats separated by whitespace, and that you want to build a list of all the numbers, you can try the following:
您可以使用:
获取仅包含所有数据行的字段值的 python 列表。
You can use:
to get a python list with only the field values for all the lines of data.
使用 python 正则表达式 模式怎么样?
对于您的情况 line.strip() 将不起作用,因为 Python仅删除前导和尾随字符
How about using a python regex pattern?
And for your case of line.strip() will not work because Python removes only the leading and trailing characters
pythons csv 库对此很有用。
http://docs.python.org/library/csv.html
CSV = 逗号分隔值,但如果设置分隔符 = \t,则它也适用于制表符分隔值。
pythons csv library is good for this.
http://docs.python.org/library/csv.html
CSV = comma seperated values, but if you set the delimiter = \t, then it works for tab separated values too.
通常,根据您阅读行的方式,为了摆脱 myline 中的 \n,
你可以乘坐我的线路[:-1]
因为 \n 是 myline 的最后一个字符。
对于“\t”,您可以使用replace()或strip()
Often, depending on the way you read the lines, in order to get rid of \n from myline,
you can take myline[:-1]
since \n is the last character of myline.
For the '\t' you can use replace() or strip()
如果您尝试转换由制表符分隔的多行浮点数,则
float(line)
会尝试将整行转换为一个浮点数,如果有多个浮点数,则会失败。使用strip
去除前导和尾随空格并不能解决这个根本问题。也许您需要将每一行
拆分
分成几部分并对每一部分执行一些操作?If you're trying to convert lines of floats separated by tab characters, then just
float(line)
will try to convert the whole line into one float, which will fail if there's more than one. Usingstrip
to get rid of leading and trailing whitespace isn't going to help that fundamental problem.Maybe you need to
split
each line into pieces and do something with each piece?