在python中处理外来字符

2024-05-05 00:31:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在处理像“€”这样的特殊角色时遇到了问题。你知道吗

文本包含不同货币和不同格式的价格

 TEXT example:
    $ 11.00 USD
    $ 9.58 USD
    559,89 pуб.
    $ 9.58
    8,10€
    7,05€
    8,10€
    CDN$ 11.00
    22,10 TL
    $ 9.58 USD

我想把不同的价格分成不同的单子

USD = []
RUS = []
EUR = []
TYR = []
CND = []
for link in TEXT:
    href = link.text.strip()
    if 'USD' in href:
        href = href.replace("$","")
        href = href.replace(" ","")
        href = href.replace("USD","")
        href = float(float(href))
        USD.append(href)
    elif 'pуб' in href:
        href = href.replace("pуб","")
        href = href.replace(" ","")
        href = href.replace(".",",")
        href = float(float(href))
        RUS.append(href)
    elif '€' in href:
        href = href.replace("€","")
        href = href.replace(" ","")
        href = href.replace(".",",")
        href = float(float(href))
        EUR.append(href)
    elif 'TL' in href:
        href = href.replace("TL","")
        href = href.replace(" ","")
        href = float(float(href))
        TYR.append(href)
    elif 'CND' in href:
        href = href.replace("CND$","")
        href = href.replace(" ","")
        href = float(float(href))
        CND.append(href)
    else:
        print("unknown currency")

但我得到了一个错误:

SyntaxError: Non-ASCII character '\xd1' in file C:/Users/S/PycharmProjects/untitled1/demo.py on line 26, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

因为它能识别符号:pбб,€。。。你知道吗


Tags: textinfor价格floateurreplaceusd