1

I'm kinda newbie to Jython. I'm trying to execute a python script through a Java program (using Jython). Inside the python script I'm trying to call a method of some external library (called petl). When I try to execute the script with python (As: python script.py) it executes without any problem. But when I try to access the script with Jython (As: jython script.py) it gives me the following error.

P.S: I can run simple python scripts without any problem. But when I try to access an external library function through the script it gives me an error.

Can anyone please give me an workaround or some advice? Thanks in advance.

Python Script:

import petl as etl
table1 = etl.fromcsv('Books.csv')
table2 = etl.sort(table1, 'ACCOUNT_ID')
etl.tocsv(table2, source='NewBooks.csv',encoding='utf-8')

Error Stack:

Traceback (most recent call last):
 File "test1.py", line 5, in <module>
 etl.tocsv(table2, source='NewBooks.csv',encoding='utf-8')
 File "C:\Jython\Lib\site-packages\petl\io\csv.py", line 106, in tocsv
 tocsv_impl(table, source=source, encoding=encoding, errors=errors,
 File "C:\Jython\Lib\site-packages\petl\io\csv_py2.py", line 50, in tocsv_impl
 _writecsv(table, source=source, mode='wb', **kwargs)
 File "C:\Jython\Lib\site-packages\petl\io\csv_py2.py", line 74, in _writecsv
 for row in rows:
 File "C:\Jython\Lib\site-packages\petl\transform\sorts.py", line 271, in _iter
nocache
 hdr = next(it)
 File "C:\Jython\Lib\site-packages\petl\io\csv_py2.py", line 30, in __iter__
 codec = getcodec(self.encoding)
 File "C:\Jython\Lib\site-packages\petl\io\base.py", line 12, in getcodec
 codec = codecs.lookup(encoding)
 at org.python.core.codecs.normalizestring(codecs.java:62)
 at org.python.core.codecs.access200ドル(codecs.java:29)
 at org.python.core.codecs$CodecState.lookup(codecs.java:1695)
 at org.python.core.codecs.lookup(codecs.java:58)
 at org.python.modules._codecs.lookup(_codecs.java:57)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:601)
java.lang.NullPointerException: java.lang.NullPointerException
asked May 25, 2015 at 19:51
10
  • Does it fail if you run it using a standalone jython executable instead of embedding it? Might be related: Unable to use the codecs module when embedding Jython Commented May 25, 2015 at 20:38
  • Yes sir, it fails to run using a standalone jython executable too. But it runs when I run it as a standalone python. (With "python script.py" command) Commented May 25, 2015 at 22:05
  • What is the output of jython -c "import codecs, sys; print codecs.lookup('utf8'), sys.path, sys.executable"? Commented May 26, 2015 at 8:09
  • Output for the above command is: <codecs.CodecInfo object for encoding utf-8 at 0x2> ['', 'C:\\Jython\\Lib\\site- packages\\jip-0.9.4-py2.7.egg', 'C:\\Jython\\Lib\\site-packages\\requests-2.7.0- py2.7.egg', 'C:\\Jython\\Lib', 'classpath', '__pyclasspath__/', 'C:\\Jython\ \Lib\\site-packages'] C:\Jython\bin\jython.exe Commented May 26, 2015 at 8:28
  • you should put the output in your question (to format it properly). Have you noticed that C:\Jython != C:\jython2.7.0? Use the right jython executable, run jython -c "import codecs; print codecs.lookup(None)". To workaround the issue, try to pass the encoding to tocsv() function explicitly (a string, not None). Commented May 26, 2015 at 8:38

1 Answer 1

1

You have to explicitly pass the value for encoding when calling both fromcsv() and tocsv() functions.

import petl as etl
table1 = etl.fromcsv(source='Books.csv',encoding='utf-8')
table2 = etl.sort(table1, 'ACCOUNT_ID')
etl.tocsv(table2, source='NewBooks.csv',encoding='utf-8')
answered May 26, 2015 at 11:25
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.