question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't add new styles in python2

See original GitHub issue

Hi, When adding a new style to the subtitles I get the following error:

Traceback (most recent call last):
  File "colorset.py", line 20, in <module>
    subs1.save(foutput,format_='ass')
  File "/home/mohammad/.local/lib/python2.7/site-packages/pysubs2/ssafile.py", line 190, in save
    self.to_file(fp, format_, fps=fps, **kwargs)
  File "/home/mohammad/.local/lib/python2.7/site-packages/pysubs2/ssafile.py", line 222, in to_file
    impl.to_file(self, fp, format_, fps=fps, **kwargs)
  File "/home/mohammad/.local/lib/python2.7/site-packages/pysubs2/substation.py", line 253, in to_file
    fields = [field_to_string(f, getattr(ev, f)) for f in EVENT_FIELDS[format_]]
  File "/home/mohammad/.local/lib/python2.7/site-packages/pysubs2/substation.py", line 242, in field_to_string
    raise TypeError("Unexpected type when writing a SubStation field")
TypeError: Unexpected type when writing a SubStation field

If I run the same code in python3 it runs fine but if i run it under python2 i get the error, I’m trying to use pysubs2 in kodi which currently only support python2. The code I used

import pysubs2
import chardet

finput='en.srt'
foutput='en.ass'
with open(finput,'rb') as fi:
	rawdata = fi.read()
	encoding = chardet.detect(rawdata)['encoding']
fi.close()

subs1 = pysubs2.load(finput)
top_style = pysubs2.SSAStyle()
top_style.alignment=8
subs1.styles['top-style'] = top_style
for line in subs1:
	line.style='top-style'
subs1.save(foutput,format_='ass')

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
moham96commented, Apr 15, 2018

@tkarabela
It is solved, I upgraded the library in the python3 site-packages (pip3) and tested on the python2 😅 It was a stupid mistake on my end.

Thank you very much for your help

1reaction
tkarabelacommented, Apr 14, 2018

I don’t have much time to develop this library anymore, but every now and then I manage to release a new version, so it’s not quite dead 😃

The best way to use pysubs2 is from Python 3 or Python 2 with from __future__ import unicode_literals. I was able to fix your particular issue, so your original code now works as expected with Python 2.7 and newly released 0.2.3 version of the library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Old-style and new-style classes in Python 2.7 - Stack Overflow
Always subclass "object". Those are new style classes. You are ready for Python 3 that way. Things like .super() work properly that way,...
Read more >
Create style. · Issue #147 · python-openxml/python-docx
I'm just learning Python. Saving default document (C:\Python27\Lib\site-packages\docx\templates) with the necessary gives nothing. Text ...
Read more >
Understanding Styles — python-docx 0.8.11 documentation
In general, the style id is formed simply by removing spaces from the localized style name, however there are exceptions. Users of python-docx...
Read more >
Python String Formatting Best Practices
Python String Formatting Best Practices · #1 “Old Style” String Formatting (% Operator) · #2 “New Style” String Formatting (str.format) · #3 String...
Read more >
Style Sheets | PyCharm Documentation - JetBrains
With PyCharm, you can write style definitions in CSS as well as in various languages that compile into it, such as Sass, Less,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found