如何删除"\ n"在列表列表中(python) [英] How to remove "\n" in a list of lists (python)

查看:1072
本文介绍了如何删除"\ n"在列表列表中(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个庞大的列表列表,我是从这样的文件中导入的:

I have a giant list of lists that I imported from a file like this:

letters = []
for i in range(len(string)):
    let = []
    for j in range(7):
        line = infile.readline()
        let = let + [line]
    letters.append(let)
    infile.readline()

它有一个很大的列表列表,但是每个辅助列表的末尾都有一个\ n.

Its a big list of lists, but each secondary list has a \n at the end of it.

[['   ###   \n', '  ## ##  \n', ' ##   ## \n', '##     ##\n', 
'#########\n', '##     ##\n', '##     ##\n'], ['######## \n',
'##     ##\n', '##     ##\n', '######## \n', '##     ##\n',
'##     ##\n', '######## \n'], ... ]]

如何删除\ n?所以只是

How do I remove the \n? so its just

[['   ###   ', '  ## ##  ', ' ##   ## ', '##     ##', 
'#########', '##     ##', '##     ##'], ['######## ',
'##     ##', '##     ##', '######## ', '##     ##',
'##     ##', '######## '], ... ]]

在其中也要有空间很重要.我已经尝试过了

It's important to have the spaces in there as well. I've tried doing

 letters.strip("\n")

但这没用.

请帮助!

我认为这可能与

line = infile.readline()

但我不确定如何解决.

我想要的输出是

    ###    #######  ########

但是我得到了这个

    ###
  #######
 ########

推荐答案

只要您当前的代码调用了.readline(),请将其命名为.readline().rstrip('\n').

Wherever your current code has a call to .readline(), make it .readline().rstrip('\n') instead.

这篇关于如何删除"\ n"在列表列表中(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆