替换 Python 列表中的特殊字符
Replacing special characters in a list in Python
我正在尝试删除列表中的特殊字符:
file_stuff
['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
file_stuff_new = [x for x in file_stuff if x != '\n']
file_stuff_new = [x.replace('\n', '') for x in file_stuff_new]
file_stuff_new
['John Smith', 'Gardener', 'Age 27', 'Englishman']
这显然有效。还有其他建议吗?
您可以使用 strip(),如:
file_stuff = map(lambda s: s.strip(), file_stuff)
print(file_stuff)
// ['John Smith', '', 'Gardener', '', 'Age 27', '', 'Englishman']
如果您想从列表中删除空项目,请使用过滤器,例如
file_stuff = filter(None, map(lambda s: s.strip(), file_stuff))
您可以尝试将您的列表映射到类似 replace 的函数:
file_stuff = map(lambda x: x.replace("\n", ""), file_stuff)
您正在使用原始字符串文字。
r'\n'
不是换行符,它是一个长度为2的字符串,包含字符“\”和"n"。
>>> r'\n'
'\n'
>>> len(r'\n')
2
否则,您原来的方法(几乎)可以正常工作。
>>> file_stuff = ['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
>>> [x.replace('\n', '') for x in file_stuff]
['John Smith', '', 'Gardener', '', 'Age 27', '', 'Englishman']
我们可以像这样过滤掉空字符串:
>>> file_stuff = ['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
>>> no_newline = (x.replace('\n', '') for x in file_stuff)
>>> result = [x for x in no_newline if x]
>>> result
['John Smith', 'Gardener', 'Age 27', 'Englishman']
其中 no_newline
是一个不构建中间临时列表的内存高效生成器。
如果您只想去除字符串开头和结尾的空格和换行符,请考虑 str.strip
方法。
>>> file_stuff = ['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
>>> no_newline = (x.strip() for x in file_stuff)
>>> result = [x for x in no_newline if x]
>>> result
['John Smith', 'Gardener', 'Age 27', 'Englishman']
这可以缩短为
>>> result = [x.strip() for x in file_stuff if x.strip()]
>>> result
['John Smith', 'Gardener', 'Age 27', 'Englishman']
如果你能解决每个字符串调用两次 str.strip
的不雅之处。
这个例子是带有条件的简单列表理解:
>>> stuff = ['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
>>> pure = [i.strip() for i in stuff if i.strip()]
>>> print(pure)
['John Smith', 'Gardener', 'Age 27', 'Englishman']
我正在尝试删除列表中的特殊字符:
file_stuff
['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
file_stuff_new = [x for x in file_stuff if x != '\n']
file_stuff_new = [x.replace('\n', '') for x in file_stuff_new]
file_stuff_new
['John Smith', 'Gardener', 'Age 27', 'Englishman']
这显然有效。还有其他建议吗?
您可以使用 strip(),如:
file_stuff = map(lambda s: s.strip(), file_stuff)
print(file_stuff)
// ['John Smith', '', 'Gardener', '', 'Age 27', '', 'Englishman']
如果您想从列表中删除空项目,请使用过滤器,例如
file_stuff = filter(None, map(lambda s: s.strip(), file_stuff))
您可以尝试将您的列表映射到类似 replace 的函数:
file_stuff = map(lambda x: x.replace("\n", ""), file_stuff)
您正在使用原始字符串文字。
r'\n'
不是换行符,它是一个长度为2的字符串,包含字符“\”和"n"。
>>> r'\n'
'\n'
>>> len(r'\n')
2
否则,您原来的方法(几乎)可以正常工作。
>>> file_stuff = ['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
>>> [x.replace('\n', '') for x in file_stuff]
['John Smith', '', 'Gardener', '', 'Age 27', '', 'Englishman']
我们可以像这样过滤掉空字符串:
>>> file_stuff = ['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
>>> no_newline = (x.replace('\n', '') for x in file_stuff)
>>> result = [x for x in no_newline if x]
>>> result
['John Smith', 'Gardener', 'Age 27', 'Englishman']
其中 no_newline
是一个不构建中间临时列表的内存高效生成器。
如果您只想去除字符串开头和结尾的空格和换行符,请考虑 str.strip
方法。
>>> file_stuff = ['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
>>> no_newline = (x.strip() for x in file_stuff)
>>> result = [x for x in no_newline if x]
>>> result
['John Smith', 'Gardener', 'Age 27', 'Englishman']
这可以缩短为
>>> result = [x.strip() for x in file_stuff if x.strip()]
>>> result
['John Smith', 'Gardener', 'Age 27', 'Englishman']
如果你能解决每个字符串调用两次 str.strip
的不雅之处。
这个例子是带有条件的简单列表理解:
>>> stuff = ['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
>>> pure = [i.strip() for i in stuff if i.strip()]
>>> print(pure)
['John Smith', 'Gardener', 'Age 27', 'Englishman']