如何删除所有\n并使其成为包含5列的csv文件?

2024-10-04 03:15:58 发布

您现在位置:Python中文网/ 问答频道 /正文

[['\nAabhas\n'], ['\n\nThe sense, Feelings (1)\n\n'], ['\n5\n'], ['\nBoy\n'], ['\n\n']

在此列表中,[]之间的值应作为一列中的值。你知道吗


Tags: 列表sensenthen5feelingsnaabhasnboy
2条回答

使用strip()

>>> l = '\nAabhas\n'
>>> l.strip()
'Aabhas'
>>> 

使用strip是最好的方法,我认为regex很昂贵-

lst = [['\nAabhas\n'], ['\n\nThe sense, Feelings (1)\n\n'], ['\n5\n'], ['\nBoy\n'], ['\n\n']]

corrected_list = []

for i in lst:
    intm = []
    for j in i:
        intm.append(j.strip())
    corrected_list.append(intm)
print corrected_list

或者使用一行列表理解-

[[j.strip() for j in i]  for i in lst]

输出-

[['Aabhas'], ['The sense, Feelings (1)'], ['5'], ['Boy'], ['']]

相关问题 更多 >