views:

114

answers:

3

I am trying to run the script csv2json.py in the Command Prompt, but I get this error:

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

Here are the relevant lines from the snippet:

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 that the directory name and file name are combined. How can I make this script run?

Thanks.

Edit:

Altering lines 31 and 32 as answered by Denis Otkidach worked fine. But I realized that the first column name needs to be pk and each row needs to start with an integer:

for row in reader:
    if not header_row:
        header_row = row
        continue

    pk = row[0]
    model = model_name
    fields = {}
    for i in range(len(row)-1):
        active_field = row[i+1]

So my csv row now looks like this (including the header row):

pk, firm_url, firm_name, first, last, school, year_graduated
1, http://www.graychase.com/aabbas, Gray & Chase, Amr A, Babas, The George Washington University Law School, 2005

Is this a requirement of the django fixture or json format? If so, I need to find a way to add the pk numbers to each row. Can I delete this pk column? Any suggestions?

Edit 2

I keep getting this ValidationError: "This value must be an integer". There is only one integer field and that's the pk. Is there a way to find out from the traceback what the line numbers refer to?

Problem installing fixture   'C:\Users\A\Documents\Projects\Django\sw2\wkw2\fixtures\csvtest1.csv.json': Traceback (most recent call last):

  File "C:\Python26\Lib\site-packages\django\core\management\commands\loaddata.py",  line 150, in handle
  for obj in objects:

  File "C:\Python26\lib\site-packages\django\core\serializers\json.py", line 41, in Deserializer
    for obj in PythonDeserializer(simplejson.load(stream)):

  File "C:\Python26\lib\site-packages\django\core\serializers\python.py", line 95, in Deserializer
    data[field.attname] = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)

  File "C:\Python26\lib\site-packages\django\db\models\fields\__init__.py", line 356, in to_python
    _("This value must be an integer."))

ValidationError: This value must be an integer.
A: 

You should be using os.path.join rather than just concatenating dirname() and filenames.

import os.path
in_file = os.path.join(dirname(__file__), input_file_name)
out_file = os.path.join(dirname(__file__), input_file_name + ".json")

will fix your problem, though depending on what exactly you're doing, there's probably a more elegant way to do it.

Paul McMillan
+1  A: 
from os import path
in_file = path.join(dirname(__file__), input_file_name )
out_file = path.join(dirname(__file__), input_file_name + ".json" )
[...]
Pydev UA
+1  A: 

+ is used incorrectly here, the proper way to combine directory name and file name is using os.path.join(). But there is no need to combine directory where script is located with file name, since it's common to pass relative path to current working directory. So, change lines 31-32 to the following:

in_file = input_file_name
out_file = in_file + '.json'
Denis Otkidach
This worked fine. Thanks!
Zeynel