Thavidu
Thavidu

Reputation: 21

Fails to run the script as Jython. But works with python without any issue

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.access$200(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

Upvotes: 1

Views: 652

Answers (1)

Thavidu
Thavidu

Reputation: 21

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')

Upvotes: 1

Related Questions