Python decode unknown character

2019-12-16 17:42发布

问题:

I'm trying to decode the following: UKLTD� For into utf-8 (or anything really) but I cannot workout how to do it and keep getting errors like

'ascii' codec can't decode byte 0xae in position 8: ordinal not in range(128)

I'm reading from a csv and have the following:

with open(path_to_file, 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        order = Order(
           ...
           product_name = row[11].encode('utf-8'),
           ...
        )
        order.save()

I would be happy right now to just ignore the character if I have keep the rest of the string.

回答1:

Thank you to @BartFriederichs.

Solution is : product_name = row[11].decode('iso-8859-1').encode('utf8')