-2

I'm trying to make a text file that contains all 54607 printable characters, but each line should only be 80 characters long for readability.

utf_all = ' !"#$'...'
lines   = '\n'.join(utf_all[i:i+80] for i in range(0, 54607, 80))
file    = open('allchars.txt', 'w').write(lines)

That returns an error message

UnicodeEncodeError: 'charmap' codec can't encode characters in position 193-243: character maps to <undefined>

If I try encoding the characters and writing in binary mode it ignores the newline \n and puts the entire string into one line and appends a newline to the end of the file.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
bup
  • 113
  • 6

1 Answers1

0

Your code, as it is, works fine for me in python3.5. However you open the file as text file, that's not what you wanted?

If I replace it with

open('allchars.txt', 'wb').write(lines)

then I had to add encode('utf-8') to lines:

file    = open('allchars.txt', 'wb').write(lines.encode('utf-8'))

EDIT: my code for this:

utf_all = ''.join([chr(i)  for i in range(2**16)])
lines   = '\n'.join(utf_all[i:i+80] for i in range(0, 54607, 80))
file    = open('allchars.txt', 'wb').write(lines.encode('utf-8'))

my text editor will open this an wrap after 80 chars (gedit)

DomTomCat
  • 8,189
  • 1
  • 49
  • 64