You might have to make sure that my indentation there is correct.

Also, I noticed something else about your code: you are assigning 2-tuples
to all of your data types rather than strings when you do
   data.PersonID=row[0],
   data.FirstName=row[1],
   data.LastName=row[2],
   data.Address=row[3],

You might actually be aware of this, but I once spent 4 hours debugging
before I realized this on a previous project.
weapons = ('fear', 'surprise', 'fanatical devotion to the pope',)
clearly creates a 3-item tuple. But so does
weapons = 'fear', 'surprise', 'fanatical devotion to the pope',
and in fact
weapons = 'fear',
creates a 1-item tuple.

On Fri, Apr 24, 2015 at 12:33 AM, Andrew Farrell <armorsmit...@gmail.com>
wrote:

> could you replace your function with
> def readcsvfile(request):
>     with open('/var/www/html/webapp/csvupload/data.csv','rb') as csvfile:
>         readata=csv.reader(csvfile,delimiter=';', quotechar='|')
>         #readata=csv.reader(csvfile)
>         for i, row in enumerate(readata):
>             try:
>                 data=test()
>                 data.PersonID=row[0],
>                 data.FirstName=row[1],
>                 data.LastName=row[2],
>                 data.Address=row[3],
>                 data.save()
>             except Exception, e:
>                 import pdb;pdb.set_trace()
>                 print i
>                 print row
>                 print row[1]
>                 raise e
>         return render_to_response('csv_test.html',locals())
>
> And then re-run this again? When you hit the offending row of data, it
> should pop you into the python debugger
> <https://docs.python.org/2/library/pdb.html>. type 'print row<enter>' and
> you should see what the problem is. If not type 'next<enter>' and step
> though the code.
>
> You can execute arbitrary python from the debugger and once you figure out
> the problem, I encourage you to play around with it. Aside from
> pedantically explaining to yourself what you think going on, pdb is one of
> the most generally useful tools.
>
> On Fri, Apr 24, 2015 at 12:01 AM, sum abiut <suab...@gmail.com> wrote:
>
>> Hi,
>> could you please help me figure out why i am getting this error i am
>> trying to import data from csv file to model buty i am getting the error: 
>> *list
>> index out of range* and if refering to line 20 on view which is:
>> *data.FirstName=row[1]*
>>
>> view.py
>> def readcsvfile(request):
>>     with open('/var/www/html/webapp/csvupload/data.csv','rb') as csvfile:
>>         readata=csv.reader(csvfile,delimiter=';', quotechar='|')
>>         #readata=csv.reader(csvfile)
>>         for row in readata:
>>             data=test()
>>             data.PersonID=row[0],
>>             data.FirstName=row[1],
>>             data.LastName=row[2],
>>             data.Address=row[3],
>>             data.save()
>>         return render_to_response('csv_test.html',locals())
>>
>>
>>
>> model.py
>>
>> class test(models.Model):
>>     PersonID = models.CharField(max_length=45)
>>     FirstName = models.CharField(max_length=45)
>>     LastName = models.CharField(max_length=45)
>>     Address = models.CharField(max_length=45)
>>
>>
>> here is the traceback:
>>
>>    -
>>    
>> /var/www/html/env/local/lib/python2.7/site-packages/django/core/handlers/base.py
>>    in get_response
>>    response = wrapped_callback(request, *callback_args,
>>    **callback_kwargs
>>
>>    <http://10.0.142.35:8000/readcsvfile/#>
>>    /var/www/html/webapp/csvapp/views.py in readcsvfile
>>    data.FirstName=row[1],
>>
>>    <http://10.0.142.35:8000/readcsvfile/#>
>>
>> Cheers,
>> Sum
>>
>>
>> On Wed, Apr 1, 2015 at 1:51 PM, sum abiut <suab...@gmail.com> wrote:
>>
>>> Thanks guys,
>>> appreciate your help. Yes i am looking to have a form that accepts CSV
>>> files. I will have a read on the link that you guys have provided and see
>>> how it goes.
>>>
>>> Cheers
>>>
>>>
>>>
>>> On Wed, Apr 1, 2015 at 11:44 AM, Andrew Farrell <armorsmit...@gmail.com>
>>> wrote:
>>>
>>>> Hello Sum,
>>>>
>>>> There are two approaches you could take.
>>>> One is to install a plugin like django-import-export
>>>> <https://django-import-export.readthedocs.org/en/latest/> and then use
>>>> it's import functionality. Its been a while since I've done this, but when
>>>> I did under django-1.4 it Just Worked. This is the best approach if you
>>>> know your data is clean and doesn't require any sort of processing before
>>>> saving it.
>>>>
>>>> The other is to write a custom manage.py command
>>>> <https://docs.djangoproject.com/en/1.7/howto/custom-management-commands/>.
>>>> This would let you load the data using a csv.DictReader
>>>> <https://docs.python.org/2/library/csv.html#csv.DictReader>, do some
>>>> logic on it, and then bulk-create
>>>> <https://docs.djangoproject.com/en/dev/ref/models/querysets/#bulk-create> 
>>>> objects
>>>> in the database. This approach has the disadvantage that if you have a bug
>>>> in your processing script, you will end up adding messy data to your
>>>> database and having to clean it up. To avoid this, you could write a unit
>>>> test case
>>>> <https://docs.djangoproject.com/en/1.7/topics/testing/overview/> which
>>>> calls your import function in its setUp() method. This testcase would
>>>> create a separate test database, import the data into it, run your test
>>>> functions, and then clear the test database. This approach would also let
>>>> you, for example, deliberately insert malformed info into the data and
>>>> verify that your import function would catch those errors.
>>>>
>>>> On Tue, Mar 31, 2015 at 6:46 PM, sum abiut <suab...@gmail.com> wrote:
>>>>
>>>>> Hi,
>>>>> Could someone please advise this i am trying to import data from csv
>>>>> file or write to database but i am confuse on how to start. i am using
>>>>> sqllite database.
>>>>>
>>>>> Cheers,
>>>>>
>>>>>
>>>>>  --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Django users" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to django-users+unsubscr...@googlegroups.com.
>>>>> To post to this group, send email to django-users@googlegroups.com.
>>>>> Visit this group at http://groups.google.com/group/django-users.
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/django-users/CAPCf-y5Nk_ATFgYk-hduiLsrxBasS%3Dzu4Qj8Gp5F9vo%3DNRwF%2BA%40mail.gmail.com
>>>>> <https://groups.google.com/d/msgid/django-users/CAPCf-y5Nk_ATFgYk-hduiLsrxBasS%3Dzu4Qj8Gp5F9vo%3DNRwF%2BA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>>  --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Django users" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to django-users+unsubscr...@googlegroups.com.
>>>> To post to this group, send email to django-users@googlegroups.com.
>>>> Visit this group at http://groups.google.com/group/django-users.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/django-users/CA%2By5TLbaBcs8%2BrJsNmXFknRGNTC5%2BOr3URs53eiFBM8GLekmcA%40mail.gmail.com
>>>> <https://groups.google.com/d/msgid/django-users/CA%2By5TLbaBcs8%2BrJsNmXFknRGNTC5%2BOr3URs53eiFBM8GLekmcA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAPCf-y74u7NDyxxG_c7rvpver7GKmg-X9CNv-ZJpmug%3Dop3LFA%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAPCf-y74u7NDyxxG_c7rvpver7GKmg-X9CNv-ZJpmug%3Dop3LFA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2By5TLY_BPTQieu%3D%3Dc_hg4ahAGJevp%2B5zb%3D1PWOw5GJ-4ULZ3w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to