Unicode incorrectly prints cp850 (cp437), suits for game cards

To summarize: How do I print the Unicode system myself to create game card characters?

What I am doing wrong, I believe that I am fairly fluent in Python, except that I do not seem to be able to type correctly!

# coding: utf-8
from __future__ import print_function
from __future__ import unicode_literals
import sys

symbols = ('♥','♦','♠','♣')
# red suits to sdterr for IDLE
print(' '.join(symbols[:2]), file=sys.stderr)
print(' '.join(symbols[2:]))

sys.stdout.write(symbols) # also correct in IDLE
print(' '.join(symbols))

Printing to the console, which is the main one for the console application, is unsuccessful, although:

J:\test>chcp
Aktiivinen koodisivu: 850


J:\test>symbol2
Traceback (most recent call last):
  File "J:\test\symbol2.py", line 9, in <module>
    print(''.join(symbols))
  File "J:\Python26\lib\encodings\cp850.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-3: character maps to <unde
fined>
J:\test>chcp 437
Aktiivinen koodisivu: 437

J:\test>d:\Python27\python.exe symbol2.py
Traceback (most recent call last):
  File "symbol2.py", line 6, in <module>
    print(' '.join(symbols))
  File "d:\Python27\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2660' in position 0: character maps
o <undefined>

J:\test>

So, summa summaryum I have a console application that runs until you use the console, but IDLE.

I can, of course, generate the characters myself by creating them using chr:

# correct symbols for cp850
print(''.join(chr(n) for n in range(3,3+4)))

But it looks a very silly way. And I do not run programs only on Windows or have many special cases (for example, conditional compilation). I need readable code.

, , , , Nokia, Windows Linux. ,

+3
4

Unicode codecs:

:

# coding: utf-8
from __future__ import print_function
import sys
import codecs

symbols = (u'♠',u'♥',u'♦',u'♣')

print(u' '.join(symbols))
print(*symbols)
with codecs.open('test.txt','w','utf-8') as testfile:
    print(*symbols, file=testfile)

# coding: utf-8
from __future__ import print_function
from __future__ import unicode_literals
import sys
import codecs

symbols = ('♠','♥','♦','♣')

print(' '.join(symbols))
print(*symbols)
with codecs.open('test.txt','w','utf-8') as testfile:
    print(*symbols, file=testfile)

print.

+1

, , UTF-8 CMD, , CMD UTF-8:
Windows, Unicode?

, , , UTF-8 .

:

  • symbols = ('♠','♥', '♦','♣'), UTF-8, Python 3, UTF-8 , u:
    symbols = (u'♠', u'♥', u'♦', u'♣')

  • str(arg) unicode , unicode(arg) unicode

  • .decode() , UTF-8, UTF-8 , .encode()

  • , open('test.txt', 'w') open('test.txt', 'wb') ( wb), ,

, :

# -*- coding: utf-8 -*-
from __future__ import print_function
import sys

symbols = (u'♠',u'♥', u'♦',u'♣')

print(' '.join(symbols))
print('Failure!')

def print(*args,**kwargs):
    end = kwargs[end] if 'end' in kwargs else '\n'
    sep = kwargs[sep] if 'sep' in kwargs else ' '
    stdout = sys.stdout if 'file' not in kwargs else kwargs['file']
    stdout.write(sep.join(unicode(arg).encode('utf-8') for arg in args))
    stdout.write(end)

print(*symbols)
print('Success!')
with open('test.txt', 'wb') as testfile:
    print(*symbols, file=testfile)

UTF-8 ( , Ubuntu ).

+1

, utf-8, :

import codecs

out = codecs.getwriter('utf-8')(sys.stdout)

str = u'♠'

out.write("%s\n" % str)

encode('utf-8') , - sdtout/stderr.

+1

UTF-8 Windows - .

1602 6058 -, , , .

:

  • 'cp65001' 'utf8' Lib/encodings/aliases.py
  • Lucida Console Consolas
  • run chcp 65001
  • python
0

Source: https://habr.com/ru/post/1775978/


All Articles