CSV to JSON snippet

222 views
Skip to first unread message

Zeynel

unread,
Nov 12, 2009, 1:38:20 PM11/12/09
to Django users
Hello,

I am trying to use this CSV to JSon Fixture snippet but I couldn't
make it work.

http://www.djangosnippets.org/snippets/1680/

Can anyone help me to run this snippet.

I copy the instruction below.

Thank you.

----------------------------------------------
The instructions for csv2json.py

To run script

csv2json.py input_file_name model_name

e.g. csv2json.py airport.csv app.airport.Airport

Note: input_file_name should be a path relative to where this script
is.

Scripts depends on simplejson module.

The module can just be placed in a sub-folder to the script to make it
easier to import.

If you use the same Python binary that you use for your Django site,
you could use the Django import instead:

from django.utils import simplejson

R. Gorman

unread,
Nov 12, 2009, 1:57:06 PM11/12/09
to Django users
What is the error message you are receiving?

R.

Zeynel

unread,
Nov 12, 2009, 2:14:26 PM11/12/09
to Django users
Hi,

my project is in C:/sw1/wkw1
models are in C:/sw1/wkw1/models.py

Model name is Lawyer.

So, in the python shell I import simplejson with

from django.utils import simplejson

then I enter

csv2json.py sw.csv wkw1.Lawyer


and I get the error

>>> from django.utils import simplejson
>>> csv2json.py sw.csv wkw1.Lawyer
File "<stdin>", line 1
csv2json.py sw.csv wkw1.Lawyer
^
SyntaxError: invalid syntax
>>>

What am I doing wrong?

BenChapman

unread,
Nov 13, 2009, 1:36:55 PM11/13/09
to Django users
At line 21 of csv2json.py, change "import simplejson" to "from
django.utils import simplejson"

Now try running the csv2json.py script from the command line:

python csv2json.py sw.csv wkw1.Lawyer

See if that solves the problem.

Best,

Ben

Zeynel

unread,
Nov 13, 2009, 10:57:07 PM11/13/09
to Django users
Thanks, I tried but this did not work either:

>>> csv2json.py sw.csv wkw1.Lawyer

File "<stdin>", line 1
csv2json.py sw.csv wkw1.Lawyer
^
SyntaxError: invalid syntax
>>>

I checked "stdin" but I didn't understand how it relates to this case:

http://en.wikibooks.org/wiki/Python_Programming/Input_and_output

http://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python

Karen Tracey

unread,
Nov 13, 2009, 11:24:13 PM11/13/09
to django...@googlegroups.com
On Fri, Nov 13, 2009 at 10:57 PM, Zeynel <azey...@gmail.com> wrote:
Thanks, I tried but this did not work either:

>>> csv2json.py sw.csv wkw1.Lawyer

 File "<stdin>", line 1
   csv2json.py sw.csv wkw1.Lawyer
                ^
SyntaxError: invalid syntax
>>>


You need to run the .py script from an OS shell (command prompt), not from within a Python shell.

Karen

Zeynel

unread,
Nov 14, 2009, 7:42:11 AM11/14/09
to Django users
Great, thank you. When I ran it in the command prompt it worked fine.
I noticed that the first column needs to be "pk" with rows starting
with integers.

On Nov 13, 11:24 pm, Karen Tracey <kmtra...@gmail.com> wrote:

Zeynel

unread,
Dec 10, 2009, 3:12:47 PM12/10/09
to Django users
This worked fine before but now I get the error

C:\...\Django\sw2\wkw2>csv2json.py csvtest1.csv wkw2.Lawyer
Converting C:\...\Django\sw2\wkw2csvtest1.csv from CSV to JSON as C:...
\Django\sw2\wkw2csvtest1.csv.json
Traceback (most recent call last):
File "C:\...\Django\sw2\wkw2\csv2json.py", line 37, in <module>
f = open(in_file, 'r' )
IOError: [Errno 2] No such file or directory: 'C:\\...\\Django\\sw2\
\wkw2cvtest1.csv'

From the snippet http://www.djangosnippets.org/snippets/1680/:

31 in_file = dirname(__file__) + input_file_name
32 out_file = dirname(__file__) + input_file_name + ".json"

34 print "Converting %s from CSV to JSON as %s" % (in_file, out_file)

36 f = open(in_file, 'r' )
37 fo = open(out_file, 'w')


It seems to combine the directory name and file name?

Thanks.

Zeynel

unread,
Dec 12, 2009, 1:04:38 PM12/12/09
to Django users
This is fixed by modifying lines 31-32: http://stackoverflow.com/questions/1894099/csv2json-py-error

On Dec 10, 3:12 pm, Zeynel <azeyn...@gmail.com> wrote:
> This worked fine before but now I get the error
>
> C:\...\Django\sw2\wkw2>csv2json.py csvtest1.csvwkw2.Lawyer
> Converting C:\...\Django\sw2\wkw2csvtest1.csvfromCSVto JSON as C:...
> \Django\sw2\wkw2csvtest1.csv.json
> Traceback (most recent call last):
>   File "C:\...\Django\sw2\wkw2\csv2json.py", line 37, in <module>
>     f = open(in_file, 'r' )
> IOError: [Errno 2] No such file or directory: 'C:\\...\\Django\\sw2\
> \wkw2cvtest1.csv'
>
> From the snippethttp://www.djangosnippets.org/snippets/1680/:
>
> 31 in_file = dirname(__file__) + input_file_name
> 32 out_file = dirname(__file__) + input_file_name + ".json"
>
> 34 print "Converting %s fromCSVto JSON as %s" % (in_file, out_file)
Reply all
Reply to author
Forward
0 new messages