rossant/ipymd

How to trace issue in notebook to be converted?

stanleyng8 opened this issue · 5 comments

I am calling the convert_files function on a notebook file but it throws the error below. Is there a simple way to trace which character in the notebook file is responsible? What does position 3074 signify? I want to fix the notebook so that this error no longer crops up.

Looking at the ipymd code and the trackback., this seems to be happening when one is writing out the output file. It's not done line by line so I can't narrow down what could have caused the issue.

I am running Python 2.7 in an Anaconda environment. The trackback is below.

  File "/Users/squark/anaconda3/envs/py27/lib/python2.7/site-packages/ipymd/core/scripts.py", line 161, in convert_files
    _save_file(file_to, to, converted, overwrite=overwrite)
  File "/Users/squark/anaconda3/envs/py27/lib/python2.7/site-packages/ipymd/core/scripts.py", line 101, in _save_file
    format_manager().save(file, contents, name=to, overwrite=overwrite)
  File "/Users/squark/anaconda3/envs/py27/lib/python2.7/site-packages/ipymd/core/format_manager.py", line 190, in save
    _write_text(file, contents)
  File "/Users/squark/anaconda3/envs/py27/lib/python2.7/site-packages/ipymd/utils/utils.py", line 133, in _write_text
    f.write(contents)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2018' in position 3074: ordinal not in range(128)

This is a single quotation mark: http://www.charbase.com/2018-unicode-left-single-quotation-mark

Can you try to run your code in a Python 3 environment?

Thanks for your reply. Indeed it works fine under Python3. Let me try round tripping and then using Python2 again to see if I got rid of the issue.

I did the conversion from notebook to markdown in Python 3 then back again. Using the cleaned up notebook, I now ran my script under Python 2 and it fails with the same error. Hmm... Unicode left single quotation mark? How can I search for it? I did a grep on my notebook and didn't find anything weird. It would be strange to run my entire project under Py2 but switch to Py3 just for generating the markdown files.

Ok. Thanks to your link. I looked for all the places where I commented. Took out all the quotation marks and it works now. Even for Py2. Guess I have to be very careful about apostrophes. :-) Thanks again.

Good! You can also have a look at this if you want to automatically get rid of the Unicode characters.