Python 带 \n 的条带

发布于 2025-01-07 04:30:08 字数 313 浏览 0 评论 0原文

这是我的问题。

我正在尝试读取文本文件,然后将行转换为浮点数。文本文件中有 \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 技术交流群。

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

发布评论

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

评论(7

筱武穆 2025-01-14 04:30:08

您应该能够使用 line.strip('\n')line.strip('\t')。但这些不会修改 line 变量...它们只是返回删除了 \n\t 的字符串。所以你必须做类似的事情

line = line.strip('\n')
line = line.strip('\t')

,这应该适用于从开始和结束处删除。如果字符串中间有 \n\t,则需要

line = line.replace('\n','')
line = line.replace('\t','')

替换 \n\ t 虚无。

You should be able to use line.strip('\n') and line.strip('\t'). But these don't modify the line variable...they just return the string with the \n and \t stripped. So you'll have to do something like

line = line.strip('\n')
line = line.strip('\t')

That should work for removing from the start and end. If you have \n and \t in the middle of the string, you need to do

line = line.replace('\n','')
line = line.replace('\t','')

to replace the \n and \t with nothingness.

臻嫒无言 2025-01-14 04:30:08

strip() 方法默认删除空格,因此无需使用 '\t' 或 '\n' 等参数调用它。然而,Python 中的字符串是不可变的并且无法修改,即 line.strip() 调用不会更改 line 对象。结果是调用返回的新字符串。

正如已经提到的,如果您从输入文件中发布一个示例,将会有所帮助。如果每一行有多个数字,则不能使用 strip() 函数。相反,您应该使用 split(),它也是一个字符串方法。

总之,假设每行包含多个由空格分隔的浮点数,并且您想要构建所有数字的列表,您可以尝试以下操作:

floats = []
with open(filename) as f:
    for line in f:
        floats.extend([float(number) for number in line.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. the line.strip() call will not change the line 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 use split(), 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:

floats = []
with open(filename) as f:
    for line in f:
        floats.extend([float(number) for number in line.split()])
忆悲凉 2025-01-14 04:30:08

您可以使用:

mylist = []
# Assuming that you have loaded data into a lines variable. 
for line in lines:
    mylist.append(line.strip().split('\t')

获取仅包含所有数据行的字段值的 python 列表。

You can use:

mylist = []
# Assuming that you have loaded data into a lines variable. 
for line in lines:
    mylist.append(line.strip().split('\t')

to get a python list with only the field values for all the lines of data.

梦醒灬来后我 2025-01-14 04:30:08

使用 python 正则表达式 模式怎么样?

import re
f = open('test.txt', 'r')
strings = re.findall(r"\S+", f.read())

对于您的情况 line.strip() 将不起作用,因为 Python仅删除前导和尾随字符

来自 Python 文档 - 使用 < 返回字符串的副本strong>删除了前导和尾随字符。如果省略 chars 或 None,则删除空白字符。如果给定且不是 None,则 chars 必须是字符串;字符串中的字符将从调用此方法的字符串的两端被剥离。

How about using a python regex pattern?

import re
f = open('test.txt', 'r')
strings = re.findall(r"\S+", f.read())

And for your case of line.strip() will not work because Python removes only the leading and trailing characters

From Python Docs - Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on.

夏末染殇 2025-01-14 04:30:08

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.

悸初 2025-01-14 04:30:08

通常,根据您阅读行的方式,为了摆脱 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()

小兔几 2025-01-14 04:30:08

如果您尝试转换由制表符分隔的多行浮点数,则 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. Using strip 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?

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