Python3中德语Umlaute的编码/解码

时间:2019-12-15 17:29:15

标签: python python-3.x string character-encoding

我正在处理几个小时的问题,但无法解决。我确定这只是一件小事,但是不知为何我做错了。

我的目标是从公共交通公司通过json获取数据,并在显示屏上显示地铁/电车的下一个出发时间。基本上,一切正常,但是当json返回变音符号(如“ü”)时,我会收到一条错误消息。有趣的是:锐利的s(ß)有效!

以下是确切的错误消息(应为“Hütteldorf”):

UnicodeEncodeError('ascii', u'H\xfctteldorf', 1, 2, 'ordinal not in range(128)')

部分代码:

...
    apiurl = 'https://www.wienerlinien.at/ogd_realtime/monitor?rbl={rbl}&sender={apikey}'

...

        for rbl in rbls:
            r = requests.get(url, timeout=10)

            ##r.encoding = 'utf-8';
            ##print(r.json())
            ##print(r.encoding)
            ##r.encoding = 'latin1'

            if requests.codes.ok:
                try:
                    for monitor in r.json()['data']['monitors']:
                        rbl.station = monitor['locationStop']['properties']['title'].encode('utf-8')
                        for line in monitor['lines']:

                            #Decoding-Problem is here - ß works, ü doesn't
                            #UnicodeEncodeError('ascii', u'H\xfctteldorf', 1, 2, 'ordinal not in range(128)')
                            rbl.name = str(line['name'])
                            rbl.direction = str(line['towards'])

                            rbl.trafficjam = line['trafficjam'] #Boolean
...

我个人认为我尝试了在Python3中发现的所有可能的事情...编码,解码......每次Sharp或umlautü都失败时。

有人可以向我提示正确的方向吗? 非常感谢你!

[编辑:] 这是完整的源代码,其中有一种解决方法(ü= ue):

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys, getopt, time
import requests
import smbus

# Define some device parameters
I2C_ADDR  = 0x27 # I2C device address, if any error, change this address to 0x3f
LCD_WIDTH = 20   # Maximum characters per line

# Define some device constants
LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line

LCD_BACKLIGHT  = 0x08  # On
#LCD_BACKLIGHT = 0x00  # Off

ENABLE = 0b00000100 # Enable bit

# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005

#Open I2C interface
bus = smbus.SMBus(1) # Rev 2 Pi uses 1

class RBL:
    id = 0
    line = ''
    station = ''
    direction = ''
    time = -1

def replaceUmlaut(s):
    s = s.replace("Ä", "Ae") # A umlaut
    s = s.replace("Ö", "Oe") # O umlaut
    s = s.replace("Ü", "Ue") # U umlaut
    s = s.replace("ä", "ae") # a umlaut
    s = s.replace("ö", "oe") # o umlaut
    s = s.replace("ü", "ue") # u umlaut
    return s

def lcd_init():
  # Initialise display
  lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off 
  lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  time.sleep(E_DELAY)

def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = the data
  # mode = 1 for data
  #        0 for command

  bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
  bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT

  # High bits
  bus.write_byte(I2C_ADDR, bits_high)
  lcd_toggle_enable(bits_high)

  # Low bits
  bus.write_byte(I2C_ADDR, bits_low)
  lcd_toggle_enable(bits_low)

def lcd_toggle_enable(bits):
  # Toggle enable
  time.sleep(E_DELAY)
  bus.write_byte(I2C_ADDR, (bits | ENABLE))
  time.sleep(E_PULSE)
  bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
  time.sleep(E_DELAY)

def lcd_string(message,line):
  # Send string to display

  message = message.ljust(LCD_WIDTH," ")

  lcd_byte(line, LCD_CMD)

  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)


def main(argv):

    apikey = False
    apiurl = 'https://www.wienerlinien.at/ogd_realtime/monitor?rbl={rbl}&sender={apikey}'

    #Time between updates
    st = 10

    # Initialise display
    lcd_init()
    lcd_string("Willkommen!",LCD_LINE_2)

    try:
        opts, args = getopt.getopt(argv, "hk:t:", ["help", "key=", "time="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-k", "--key"):
            apikey = arg
        elif opt in ("-t", "--time"):
            try:
                tmpst = int(arg)
                if tmpst > 0:
                    st = tmpst
            except ValueError:
                usage()
                sys.exit(2)


    if apikey == False or len(args) < 1:
        usage()
        sys.exit()

    rbls = []
    for rbl in args:
        tmprbl = RBL()
        tmprbl.id = rbl
        rbls.append(tmprbl)

    x = 1
    while True:
        for rbl in rbls:
            url = apiurl.replace('{apikey}', apikey).replace('{rbl}', rbl.id)
            r = requests.get(url, timeout=10)
            r.encoding = 'utf-8'

            if requests.codes.ok:
                try:
                    for monitor in r.json()['data']['monitors']:
                        rbl.station = monitor['locationStop']['properties']['title']
                        for line in monitor['lines']:

                            rbl.name = replaceUmlaut(str(line['name'].encode('ascii','xmlcharrefreplace').decode('ascii')))
                rbl.direction = replaceUmlaut(str(line['towards'].encode('ascii','xmlcharrefreplace').decode('ascii')))

                            rbl.trafficjam = line['trafficjam']
                            rbl.type = line['type']
                            rbl.time1 = line['departures']['departure'][0]['departureTime']['countdown']
                            rbl.time2 = line['departures']['departure'][1]['departureTime']['countdown']
                            rbl.time3 = line['departures']['departure'][2]['departureTime']['countdown']

                            lcdShow(rbl)
                            time.sleep(st)

                except Exception as e:
                    print("Fehler (Exc): " + repr(e))
                    print(r)
                    lcd_string("Fehler (Exc):",LCD_LINE_1)
                    lcd_string(repr(e),LCD_LINE_2)
                    lcd_string("",LCD_LINE_3)
                    lcd_string("",LCD_LINE_4)
            else:
                print('Fehler bei Kommunikation mit Server')
                lcd_string("Fehler:",LCD_LINE_1)
                lcd_string("Serverkomm.",LCD_LINE_2)
                lcd_string("",LCD_LINE_3)
                lcd_string("",LCD_LINE_4)

def lcdShow(rbl):
    lcdLine1 = rbl.name + ' ' + rbl.station
    lcdLine2 = rbl.direction

    lcdLine3 = "".ljust(LCD_WIDTH-9) + ' ' + '{:0>2d}'.format(rbl.time1) + ' ' + '{:0>2d}'.format(rbl.time2) + ' ' + '{:0>2d}'.format(rbl.time3)

    if not rbl.type == "ptMetro":
        if rbl.trafficjam:
            lcdLine4 = "Stau in Zufahrt"
        else:
            lcdLine4 = "kein Stau"
    else:
        lcdLine4 = ""

    lcd_string(lcdLine1,LCD_LINE_1)
    lcd_string(lcdLine2,LCD_LINE_2)
    lcd_string(lcdLine3,LCD_LINE_3)
    lcd_string(lcdLine4,LCD_LINE_4)

    #print(lcdLine1 + '\n' + lcdLine2+ '\n' + lcdLine3+ '\n' + lcdLine4)

def usage():
    print('usage: ' + __file__ + ' [-h] [-t time] -k apikey rbl [rbl ...]\n')
    print('arguments:')
    print('  -k, --key=\tAPI key')
    print('  rbl\t\tRBL number\n')
    print('optional arguments:')
    print('  -h, --help\tshow this help')
    print('  -t, --time=\ttime between station updates in seconds, default 10')

if __name__ == "__main__":
    main(sys.argv[1:])

1 个答案:

答案 0 :(得分:1)

  

我个人认为我尝试了在Python3中发现的所有可能的事情...编码,解码......每次Sharp或umlautü都失败时。

如评论中所述,您似乎正在根据看到的错误消息运行Python 2。

Python 2有两种“字符串”类型,str包含原始字节,unicode包含unicode字符。调用.json()时,您将返回一个包含unicode字符串的数据结构。因此line['name']是这样的unicode字符串。

调用str(line['name'])时,您隐式要求将unicode字符串编码 ASCII 字节序列中。这将失败,因为ASCII无法表示这些字符。不幸的是,我不知道您为什么要在这里为什么rbl.name是否需要为str?在哪里使用?

其他代码期望使用哪种编码?

在评论中,Jorropo建议编写line['name'].decode("utf-8"),您指出这也不起作用。这是因为 de 编码unicode字符串实际上没有任何意义,但是Python 2会通过先 en 对其进行ASCII编码来尝试尝试(失败),然后尝试按照您的要求在UTF-8中解码。

您的修复将取决于您对rbl.name所做的工作。您可能会:

  1. 只需直接使用unicode字符串。 rbl.name = line['name']这要求随后的代码需要一个unicode字符串。
  2. 将其编码为UTF-8字节。 rbl.name = line['name'].encode('utf-8')这要求后面的代码期望序列为UTF-8字节。

无论哪种方式,当您尝试其中的任何一种时,有可能(甚至可能)其他事物随后都会中断,这完全取决于其余代码对rbl.name应该是什么以及如何做出假设。它是经过编码的。

关于它为什么可以与u'Westbahnstraße'配合使用,我不能肯定地说。您能否提供包含输入数据的完整示例,以证明一个有效而另一个无效?