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 如何从字符串中删除或转换多个字符串?_Python_String - Fatal编程技术网

Python 如何从字符串中删除或转换多个字符串?

Python 如何从字符串中删除或转换多个字符串?,python,string,Python,String,我有一根像这样的长绳子: '[("He tended to be helpful, enthusiastic, and encouraging, even to studentsthat didn\'t have very much innate talent.\\n",), (\'Great instructor\\n\',), (\'He could always say something nice and was always helpful.\\n\',), (\'He knew w

我有一根像这样的长绳子:

 '[("He tended to be helpful, enthusiastic, and encouraging, even to studentsthat didn\'t have very much innate talent.\\n",), (\'Great instructor\\n\',), (\'He could always say something nice and was always helpful.\\n\',), (\'He knew what he was doing.\\n\',), (\'Likes art\\n\',), (\'He enjoys the classwork.\\n\',), (\'Good discussion of ideas\\n\',), (\'Open-minded\\n\',), (\'We learned stuff without having to take notes, we just applied it to what we were doing; made it an interesting and fun class.\\n\',), (\'Very kind, gave good insight on assignments\\n\',), (\' Really pushed me in what I can do; expanded how I thought about art, the materials used, and how it was visually.\\n\',)
我想一次从这个字符串中删除所有[,(,,,,\,\n。我可以一个接一个地删除,但总是以“\n”失败。有什么有效的方法可以删除或翻译所有这些字符或空行符号吗?
因为我的senectics不长,所以我不想像前面的问题那样使用字典方法。

如果字符串是正确的python表达式,那么您可以使用ast模块中的literal\u eval将字符串转换为元组,然后您可以处理每个元组

from ast import literal_eval


' '.join(el[0].strip() for el in literal_eval(your_string))
如果没有,则可以使用此选项:

def get_part_string(your_string):
    for part in re.findall(r'\((.+?)\)', your_string):
        yield re.sub(r'[\"\'\\\\n]', '', part).strip(', ')

''.join(get_part_string(your_string))

也许您可以使用正则表达式查找所有要替换的字符

s = s.strip()
r = re.compile("\[|\(|\)|\]|\\|\"|'|,")
s = re.sub(r, '', s)
print s.replace("\\n", "")

我对“\n”有一些问题,但在正则表达式之后替换也很容易删除。

如何获得这样的字符串的可能重复?