Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 [u';\r\n';,u';\r\n费萨尔镇B座';,u';\r\n传真:(042)516 5232\r\n';]_Python_String_List - Fatal编程技术网

Python [u';\r\n';,u';\r\n费萨尔镇B座';,u';\r\n传真:(042)516 5232\r\n';]

Python [u';\r\n';,u';\r\n费萨尔镇B座';,u';\r\n传真:(042)516 5232\r\n';],python,string,list,Python,String,List,我想从python列表中删除空格和垃圾,如\n、\r、u等 我曾尝试使用strip函数,但没有任何结果。使用(映射、筛选列表项)和(删除字符串周围的空格,包括\r,\n): 使用(映射、筛选列表项)和(删除字符串周围的空格,包括\r,\n): location = str(response.xpath('//*[@id="addresses"]/address[2]/text()').extract()).strip("1234567890=,/\n\ru") >>> res

我想从python列表中删除空格和垃圾,如\n、\r、u等

我曾尝试使用
strip
函数,但没有任何结果。

使用(映射、筛选列表项)和(删除字符串周围的空格,包括
\r
\n
):

使用(映射、筛选列表项)和(删除字符串周围的空格,包括
\r
\n
):

location = str(response.xpath('//*[@id="addresses"]/address[2]/text()').extract()).strip("1234567890=,/\n\ru")
>>> result = [u'\r\n ', u'\r\n Block-B, Faisal Town', u'\r\n Fax: (042) 516 5232\r\n ']
>>> result = [x.strip() for x in result]
>>> result = [x for x in result if x]
>>> result
[u'Block-B, Faisal Town', u'Fax: (042) 516 5232']