Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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_Encoding_Utf 8 - Fatal编程技术网

Python 从文本文件中删除所有引号字符

Python 从文本文件中删除所有引号字符,python,string,encoding,utf-8,Python,String,Encoding,Utf 8,我正在读一个使用普通python文本编码的utf8文件。我还需要删除文件中的所有引号。然而,utf8代码有多种类型的引号,我不知道如何去掉它们。下面的代码是我一直尝试做的一个例子 def change_things(string, remove): for thing in remove: string = string.replace(thing, remove[thing]) return string 在哪里 不幸的是,这段代码只删除普通引号,而不是左引号

我正在读一个使用普通python文本编码的utf8文件。我还需要删除文件中的所有引号。然而,utf8代码有多种类型的引号,我不知道如何去掉它们。下面的代码是我一直尝试做的一个例子

def change_things(string, remove):
    for thing in remove:
        string = string.replace(thing, remove[thing])
    return string
在哪里


不幸的是,这段代码只删除普通引号,而不是左引号或右引号。是否有任何方法可以使用与我所做的类似的格式删除所有此类引号(我知道还有其他更有效的方法可以从字符串中删除项,但鉴于代码的整体上下文,这对我的特定项目更有意义)?

您只需在文件中键入这些类型,并将其替换为与任何其他字符相同的字符

utf8_quotes = "“”‘’‹›«»"
mystr = 'Text with “quotes”'
mystr.replace('“', '"').replace('”', '"')

还有一些不同的单引号变体。

上有一个unicode引号列表。这应该允许您删除任何类型的引号。

有多种方法可以做到这一点,正则表达式就是其中之一:

import re
newstr = re.sub(u'[\u201c\u201d\u2018\u2019]', '', oldstr)
另一个干净的方法是使用。这不会直接删除引号,而是将它们转换为中性引号。它还将任何非ASCII字符转换为最接近的ASCII等效字符:

from unidecode import unidecode
newstr = unidecode(oldstr)

然后,您可以用代码删除引号。

好主意。您可能想查找“智能引号”,但我不记得它们的unicode表示法。另外,可能已经有一些函数可以实现这一点。
from unidecode import unidecode
newstr = unidecode(oldstr)