Python无法识别引号

时间:2017-07-13 02:18:44

标签: python regex string encoding quotes

我想要做的非常直截了当。

  1. 从文件中检索文字
  2. 如果文本包含任何引号,请在引号内获取文本。
  3. 要做到这一点,我正在使用这个正则表达式,借用另一篇文章。

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

    所以我认为我的问题与编码有关。那就是说type(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. 
    

1 个答案:

答案 0 :(得分:0)

解决方案我发现感谢@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.