byroot / pysrt

Python parser for SubRip (srt) files
GNU General Public License v3.0
451 stars 69 forks source link

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' #82

Closed hvdwolf closed 4 years ago

hvdwolf commented 4 years ago

Tested with pysrt 1.0.1 (Debian buster 10.3 and Ubuntu Bionic 18.04 default pkg) and pypip version which is also 1.0.1 (for both debian buster and ubuntu bionic) I tried with the github version (1.1.2) but somehow I can't make that one work.

I have a series of 6 utf-8 based srt files in Dutch. If I check them I see

file -i new/Shetland-05x02-Episode2.MTB.srt 
new/Shetland-05x02-Episode2.MTB.srt: text/plain; charset=utf-8

If I try to convert them, I get:

srt shift -36s500ms checkfile.srt > checkfile2.srt 
Traceback (most recent call last):
  File "/usr/bin/srt", line 9, in <module>
    load_entry_point('pysrt==1.0.1', 'console_scripts', 'srt')()
  File "/usr/lib/python2.7/dist-packages/pysrt/commands.py", line 215, in main
    SubRipShifter().run(sys.argv[1:])
  File "/usr/lib/python2.7/dist-packages/pysrt/commands.py", line 136, in run
    self.arguments.action()
  File "/usr/lib/python2.7/dist-packages/pysrt/commands.py", line 155, in shift
    self.input_file.write_into(self.output_file)
  File "/usr/lib/python2.7/dist-packages/pysrt/srtfile.py", line 247, in write_into
    output_file.write(string_repr)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 35: ordinal not in range(128)

The character is "é" in below string (I assume): Oké, kom mee.

I already tried multiple iconv conversions like

iconv -f utf-8 -t utf-8 file1.srt > file2.srt
iconv -f utf-8 -t iso-8859-1 file1.srt > file2.srt
iconv -f iso-8859-1 -t utf-8 file1.srt > file2.srt

and more (nonsense) conversions, just to try. But none works.

My default python is Python 2.7.16. When changing to python3 (3.7.3), I get ModuleNotFoundError: No module named 'pysrt'

hvdwolf commented 4 years ago

And 20 minutes later ..... :( After having downloaded the git repo, I did not uninstall the pip and pip3 installed versions correctly. After having done that and do a sudo python3 ./setup.py install from the git repo, it installs correctly and works correctly.

Sorry for the incorrect issue.