Greetings How to make a python read from a file and display any unicode characters? Type of such all sorts of things. ← ↑ β†’ ↓ β†”β†•β†–β†—β†˜β†™β†šβ†›β†œβ†β†žβ†Ÿ. When trying to just read

# -*- coding: utf-8 -*- f = open("unicode_symbols.txt", "r") for s in f: print(u""+s) print("\n") 

displays the following error

  return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 18: character maps to <undefined> 
  • In what encoding is the text in the file encoded? If the encoding says nothing to you, then simply insert the bytes of the minimum file that leads to the error: print(open("unicode_symbols.txt", "rb").read()) - jfs
  • I like codecs.open - eri
  • @eri: codecs.open() not needed for Python 2 ( io.open() ) or Python 3 (where io.open() is available as built-in open() ) - jfs
  • @jfs semantics is different - eri
  • @eri: right, codecs.open is worse. - jfs

1 answer 1

Try first to specify the file encoding when reading explicitly.

 f = open("unicode_symbols.txt", "r", encoding = 'utf-8') 

and - everything will turn out! enter image description here

Well, if you do not believe - try another code like this:

 # BEGIN NUMERICS_DEMO import unicodedata sample = '1\xbc\xb2\u0969\u136b\u216b\u2466\u2480\u3285' f = open("F:\\PyCodes\\WOW\\utf8_rows.txt", "r+", encoding='utf-8') for char in sample: _ = 'U+%04x' % ord(char) + char.center(6) + unicodedata.name(char) f.write(_) for s in f: print(u""+s) # END NUMERICS_DEMO 

Well, of course - draw the path to your file = D I have to print this:

 U+0031 1 DIGIT ONE U+00bc ΒΌ VULGAR FRACTION ONE QUARTER U+00b2 Β² SUPERSCRIPT TWO U+0969 ΰ₯© DEVANAGARI DIGIT THREE U+136b ፫ ETHIOPIC DIGIT THREE U+216b β…« ROMAN NUMERAL TWELVE U+2466 ⑦ CIRCLED DIGIT SEVEN U+2480 β’€ PARENTHESIZED NUMBER THIRTEEN U+3285 γŠ… CIRCLED IDEOGRAPH SIX 
  • tried already, useless - Art
  • @Art and for me it works))) - Vasyl Kolomiets
  • @Art then give the exact contents of your file in bytes (not characters) - andreymal
  • no, better you) - Vasyl Kolomiets