Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中替换列表中的特殊字符_Python_Replace_Special Characters_List Comprehension - Fatal编程技术网

在Python中替换列表中的特殊字符

在Python中替换列表中的特殊字符,python,replace,special-characters,list-comprehension,Python,Replace,Special Characters,List Comprehension,我正在尝试清除列表中的特殊字符: 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'

我正在尝试清除列表中的特殊字符:

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 = filter(None, map(lambda s: s.strip(), file_stuff))

您可以尝试将列表映射到类似replace的函数:

file_stuff = map(lambda x: x.replace("\n", ""), file_stuff)

您正在使用原始字符串文字

r'\n'
不是换行符,它是一个长度为2的字符串,包含字符“\”和“n”

否则,您最初的方法(几乎)可以正常工作

我们可以像这样过滤掉空字符串:

>>> 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.Outs< /Cult>方法.< /P>

>>> 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']

请注意,列表理解将生成列表,而不是修改列表。您是否正在将列表理解的结果存储在某个地方并查看该结果?的可能重复项请使用您期望的输出更新您的问题。您可以添加
过滤器(无,文件内容)
以删除空字符串注意:在python3中,您需要用
列表()括起地图对象
@SudhirBastakoti只是稍微润色一下。很好的解决方案!我想,与其调用两次
strip()
,不如使用正则表达式来删除需要删除的内容。但不确定性能。@ChatterOne您也可以使用
result=[y for x in file.[x.strip()]中的stuff for y in[x.strip()]如果y]
,但这只会让事情变得更糟。出于教育方面的原因,我又加入了最后一个,以说明为什么我认为创建生成器是一个好方法。我认为它肯定比映射/过滤器链更具可读性。对于这项任务来说,正则表达式可能有点过分,但肯定会起作用。我认为您应该颠倒参数的顺序
>>> 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']
>>> 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']