Python不识别引号
我想做的是非常直截了当的Python不识别引号,python,regex,string,encoding,quotes,Python,Regex,String,Encoding,Quotes,我想做的是非常直截了当的 从文件中检索文本 如果文本包含任何引号,请获取引号内的文本 为了做到这一点,我使用这个正则表达式,从另一篇文章中借用 re.findall('"([^"]*)"', text) 不过,我遇到的问题是,文本文件中包含的特定引号没有被识别为引号 例如: text = #get text from a file print(text) #Outputs: 'this is a "test"' print(re.findall('"([^"]*)"', text)) #
re.findall('"([^"]*)"', text)
不过,我遇到的问题是,文本文件中包含的特定引号没有被识别为引号
例如:
text = #get text from a file
print(text)
#Outputs: 'this is a "test"'
print(re.findall('"([^"]*)"', text))
#Outputs: []
但是,如果我直接将字符串作为变量输入,它将正常工作
text = 'this is a "test"'
#The same regex outputs ['test']
所以我相信这里的问题与编码有关。所述类型(text)返回str
编辑:由于@rmharrison,我找到了解决方案
这就是现在的工作
import re
from unidecode import unidecode
text = # Text From File
cleaned_text = unidecode(text)
print(re.findall('"([^"]*)"', cleaned_text))
#This successfully outputs text inside quotes.
import re
from unidecode import unidecode
text = # Text From File
cleaned_text = unidecode(text)
print(re.findall('"([^"]*)"', cleaned_text))
#This successfully outputs text inside quotes.
多亏@rmharrison,我找到了解决方案,这就是现在的效果
import re
from unidecode import unidecode
text = # Text From File
cleaned_text = unidecode(text)
print(re.findall('"([^"]*)"', cleaned_text))
#This successfully outputs text inside quotes.
import re
from unidecode import unidecode
text = # Text From File
cleaned_text = unidecode(text)
print(re.findall('"([^"]*)"', cleaned_text))
#This successfully outputs text inside quotes.
也许你用的是卷曲引号你用的是蟒蛇2还是蟒蛇3?还有,
print(repr(text))
揭示了什么?请提供一个简短的可复制测试用例。请参阅了解原因和方法。我正在使用Python3,print(repr(text))显示“这是一个“测试”,我让它工作了。请参见上面的编辑