Python:用换行符附加列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39155665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: Append a list with a newline
提问by Steve
In the following code I am updating a list with two items by appending to it from within a for loop. I need a newline to be added after appending each string but I can't seem to be able to do it.
在下面的代码中,我通过从 for 循环中附加到列表来更新包含两个项目的列表。我需要在附加每个字符串后添加一个换行符,但我似乎无法做到。
I thought it would have been:
我以为它会是:
lines = []
for i in range(10):
line = ser.readline()
if line:
lines.append(line + '\n') #'\n' newline
lines.append(datetime.now())
But this only adds the '\n' as a string. I have also tried the same without without the quotes, but no luck.
但这只会将 '\n' 添加为字符串。我也尝试过没有引号的情况,但没有运气。
I am getting this:
我得到这个:
['leftRaw; 928090; 0; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00;\n', datetime.datetime(2016, 8, 25, 23, 48, 4, 517000), '\r\x00rightRaw; 928090; 0; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00;\n', datetime.datetime(2016, 8, 25, 23, 48, 4, 519000), '\r\x00leftRaw; 928091; 0; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00)]
But I want this:
但我想要这个:
['leftRaw; 928090; 0; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00;\n',
datetime.datetime(2016, 8, 25, 23, 48, 4, 517000),
'\r\x00rightRaw; 928090; 0; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00;\n',
datetime.datetime(2016, 8, 25, 23, 48, 4, 519000)]
Any ideas?
有任何想法吗?
回答by Steve
I fixed the issue by appending 'datetime.now' to the list as a string on every frame using the 'strftime' method. I was then able to add newlines with 'lines = '\n'.join(lines)'. See code below for the working code.
我通过使用 'strftime' 方法将 'datetime.now' 作为每个帧上的字符串附加到列表中来解决这个问题。然后我就可以用 'lines = '\n'.join(lines)' 添加换行符。有关工作代码,请参阅下面的代码。
lines = []
for frame in range(frames):
line = ser.readline()
if line:
lines.append(line)
lines.append(datetime.now().strftime('%H:%M:%S'))
lines = '\n'.join(lines)
dataFile.write('%s'%(lines))
This gives me the desired output of each list item on a new line e.g.
这为我提供了新行上每个列表项的所需输出,例如
['leftRaw; 928090; 0; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00;\n',
datetime.datetime(2016, 8, 25, 23, 48, 4, 517000),
'\r\x00rightRaw; 928090; 0; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00; 0.00;\n',
datetime.datetime(2016, 8, 25, 23, 48, 4, 519000)']
回答by anderson_berg
You can add all items to the list, then use .join() function to add new line between each item in the list:
您可以将所有项目添加到列表中,然后使用 .join() 函数在列表中的每个项目之间添加新行:
for i in range(10):
line = ser.readline()
if line:
lines.append(line)
lines.append(datetime.now())
final_string = '\n'.join(lines)
回答by Jake Askeland
It looks like the lineslist is simple being printed in its entirety. The \nyou see is only printing as an indicator that there are in fact new-lines. See this smaller example:
看起来整个lines列表很简单。在\n你看到的只是作为一个指标进行打印,有事实上的新线。请参阅这个较小的示例:
In [18]: print ['asdf' + '\n\n'] + [datetime.now()],
['asdf\n\n', datetime.datetime(2016, 8, 25, 15, 32, 5, 955895)]
In [19]: a = ['asdf' + '\n\n', datetime.now()]
In [20]: print a[0]
asdf
In [21]: print a
['asdf\n\n', datetime.datetime(2016, 8, 25, 15, 37, 14, 330440)]
In [22]:

