处理python中的外来字符

时间:2015-02-24 18:11:05

标签: python foreign-keys special-characters

我在处理像'€'这样的特殊字符时遇到了问题。

TEXT包含不同货币和不同格式的价格

 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уб,€...

1 个答案:

答案 0 :(得分:0)

指定源编码:

# -*- coding: utf-8 -*-