Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38658 > unrolled thread

Error in reading and writing CSV format file in python

Started byinshu chauhan <insideshoes@gmail.com>
First post2013-02-11 12:00 +0100
Last post2013-02-11 14:09 +0100
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Error in reading and writing CSV format file in python inshu chauhan <insideshoes@gmail.com> - 2013-02-11 12:00 +0100
    Re: Error in reading and writing CSV format file in python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-11 23:26 +1100
      Re: Error in reading and writing CSV format file in python inshu chauhan <insideshoes@gmail.com> - 2013-02-11 13:44 +0100
      Re: Error in reading and writing CSV format file in python MRAB <python@mrabarnett.plus.com> - 2013-02-11 13:02 +0000
      Re: Error in reading and writing CSV format file in python inshu chauhan <insideshoes@gmail.com> - 2013-02-11 14:09 +0100

#38658 — Error in reading and writing CSV format file in python

Frominshu chauhan <insideshoes@gmail.com>
Date2013-02-11 12:00 +0100
SubjectError in reading and writing CSV format file in python
Message-ID<mailman.1634.1360580421.2939.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

In the programme below I am trying to read two csv format files and process
them and write a new file with some of theirs data.

import csv
f1_reader = csv.reader(open(r"Z:\Weka
work\Feature_Vectors_Fullset_00.arff"))
f2_reader = csv.reader(open(r"Z:\Weka
work\Feature_Vectors_Fullset_00_noxy+class.arff"))
nf = open(r"Z:\Weka work\classified_image00_withoutxy.arff", "w")

while True:
    l1 = f1_reader.next()
    while len(l1) != 12:
        l1 = f1_reader.next()
    l2 = f2_reader.next()
    while len(l2) != 11:
        l2 = f2_reader.next()

    ix = l1[0].strip()
    iy = l1[1].strip()
    classification = l2[8].strip()

    print >> nf, ix, iy, classification

nf.close()

This programme is giving me this error now :

Traceback (most recent call last):
  File "Z:\Weka work\final_image_classificationwithoutxy.py", line 16, in
<module>
    l2 = f2_reader.next()
StopIteration


what could be a possible reason to StopIteration ???


I checked the syntax and usage of this module looks alright to me , but
then why this error ?


Thankyou in Advance

[toc] | [next] | [standalone]


#38666

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-02-11 23:26 +1100
Message-ID<5118e383$0$29985$c3e8da3$5496439d@news.astraweb.com>
In reply to#38658
inshu chauhan wrote:

> In the programme below I am trying to read two csv format files and
> process them and write a new file with some of theirs data.
> 
> import csv
> f1_reader = csv.reader(open(r"Z:\Weka
> work\Feature_Vectors_Fullset_00.arff"))
> f2_reader = csv.reader(open(r"Z:\Weka
> work\Feature_Vectors_Fullset_00_noxy+class.arff"))
> nf = open(r"Z:\Weka work\classified_image00_withoutxy.arff", "w")
> 
> while True:
>     l1 = f1_reader.next()
>     while len(l1) != 12:
>         l1 = f1_reader.next()
>     l2 = f2_reader.next()
>     while len(l2) != 11:
>         l2 = f2_reader.next()
> 
>     ix = l1[0].strip()
>     iy = l1[1].strip()
>     classification = l2[8].strip()
> 
>     print >> nf, ix, iy, classification
> 
> nf.close()
> 
> This programme is giving me this error now :
> 
> Traceback (most recent call last):
>   File "Z:\Weka work\final_image_classificationwithoutxy.py", line 16, in
> <module>
>     l2 = f2_reader.next()
> StopIteration
> 
> 
> what could be a possible reason to StopIteration ???

next() raises StopIteration when there is nothing else to return.


py> it = iter([1, 2, 3])
py> it.next()
1
py> it.next()
2
py> it.next()
3
py> it.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration


You have reached the end of the file and there is nothing else for the CSV
reader to return, so it raises StopIteration.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#38672

Frominshu chauhan <insideshoes@gmail.com>
Date2013-02-11 13:44 +0100
Message-ID<mailman.1642.1360586648.2939.python-list@python.org>
In reply to#38666

[Multipart message — attachments visible in raw view] — view raw

On Mon, Feb 11, 2013 at 1:26 PM, Steven D'Aprano <
steve+comp.lang.python@pearwood.info> wrote:

> inshu chauhan wrote:
>
> > In the programme below I am trying to read two csv format files and
> > process them and write a new file with some of theirs data.
> >
> > import csv
> > f1_reader = csv.reader(open(r"Z:\Weka
> > work\Feature_Vectors_Fullset_00.arff"))
> > f2_reader = csv.reader(open(r"Z:\Weka
> > work\Feature_Vectors_Fullset_00_noxy+class.arff"))
> > nf = open(r"Z:\Weka work\classified_image00_withoutxy.arff", "w")
> >
> > while True:
> >     l1 = f1_reader.next()
> >     while len(l1) != 12:
> >         l1 = f1_reader.next()
> >     l2 = f2_reader.next()
> >     while len(l2) != 11:
> >         l2 = f2_reader.next()
> >
> >     ix = l1[0].strip()
> >     iy = l1[1].strip()
> >     classification = l2[8].strip()
> >
> >     print >> nf, ix, iy, classification
> >
> > nf.close()
> >
> > This programme is giving me this error now :
> >
> > Traceback (most recent call last):
> >   File "Z:\Weka work\final_image_classificationwithoutxy.py", line 16, in
> > <module>
> >     l2 = f2_reader.next()
> > StopIteration
> >
> >
> > what could be a possible reason to StopIteration ???
>
> next() raises StopIteration when there is nothing else to return.
>
>
> py> it = iter([1, 2, 3])
> py> it.next()
> 1
> py> it.next()
> 2
> py> it.next()
> 3
> py> it.next()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> StopIteration
>
>
> You have reached the end of the file and there is nothing else for the CSV
> reader to return, so it raises StopIteration.
>


But why does it has nothing to return so early before traversing the whole
file ? Is there any way it can be corrected ?  And also the programme isn't
writing anything to the file ?

>
>
>

[toc] | [prev] | [next] | [standalone]


#38675

FromMRAB <python@mrabarnett.plus.com>
Date2013-02-11 13:02 +0000
Message-ID<mailman.1644.1360587743.2939.python-list@python.org>
In reply to#38666
On 2013-02-11 12:44, inshu chauhan wrote:
>
> On Mon, Feb 11, 2013 at 1:26 PM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info
> <mailto:steve+comp.lang.python@pearwood.info>> wrote:
>
>     inshu chauhan wrote:
>
>      > In the programme below I am trying to read two csv format files and
>      > process them and write a new file with some of theirs data.
>      >
>      > import csv
>      > f1_reader = csv.reader(open(r"Z:\Weka
>      > work\Feature_Vectors_Fullset_00.arff"))
>      > f2_reader = csv.reader(open(r"Z:\Weka
>      > work\Feature_Vectors_Fullset_00_noxy+class.arff"))
>      > nf = open(r"Z:\Weka work\classified_image00_withoutxy.arff", "w")
>      >
>      > while True:
>      >     l1 = f1_reader.next()
>      >     while len(l1) != 12:
>      >         l1 = f1_reader.next()
>      >     l2 = f2_reader.next()
>      >     while len(l2) != 11:
>      >         l2 = f2_reader.next()
>      >
>      >     ix = l1[0].strip()
>      >     iy = l1[1].strip()
>      >     classification = l2[8].strip()
>      >
>      >     print >> nf, ix, iy, classification
>      >
>      > nf.close()
>      >
>      > This programme is giving me this error now :
>      >
>      > Traceback (most recent call last):
>      >   File "Z:\Weka work\final_image_classificationwithoutxy.py",
>     line 16, in
>      > <module>
>      >     l2 = f2_reader.next()
>      > StopIteration
>      >
>      >
>      > what could be a possible reason to StopIteration ???
>
>     next() raises StopIteration when there is nothing else to return.
>
>
>     py> it = iter([1, 2, 3])
>     py> it.next()
>     1
>     py> it.next()
>     2
>     py> it.next()
>     3
>     py> it.next()
>     Traceback (most recent call last):
>        File "<stdin>", line 1, in <module>
>     StopIteration
>
>
>     You have reached the end of the file and there is nothing else for
>     the CSV
>     reader to return, so it raises StopIteration.
>
>
>
> But why does it has nothing to return so early before traversing the
> whole file ? Is there any way it can be corrected ?  And also the
> programme isn't writing anything to the file ?
>
Try adding some logging so that you can see what it's doing. A simple way
would be something like:

log_file = open(r"Z:\Weka work\log.txt", "w")

...

l1 = f1_reader.next()
print >> log_file, "Read from f1:", l1
print >> log_file, "Length is", len(l1)
while len(l1) != 12:
     l1 = f1_reader.next()
     print >> log_file, "Read from f1:", l1
     print >> log_file, "Length is", len(l1)

and so on.

[toc] | [prev] | [next] | [standalone]


#38676

Frominshu chauhan <insideshoes@gmail.com>
Date2013-02-11 14:09 +0100
Message-ID<mailman.1645.1360588193.2939.python-list@python.org>
In reply to#38666

[Multipart message — attachments visible in raw view] — view raw

On Mon, Feb 11, 2013 at 2:02 PM, MRAB <python@mrabarnett.plus.com> wrote:

> On 2013-02-11 12:44, inshu chauhan wrote:
>
>>
>> On Mon, Feb 11, 2013 at 1:26 PM, Steven D'Aprano
>> <steve+comp.lang.python@**pearwood.info<steve%2Bcomp.lang.python@pearwood.info>
>> <mailto:steve+comp.lang.**python@pearwood.info<steve%2Bcomp.lang.python@pearwood.info>>>
>> wrote:
>>
>>     inshu chauhan wrote:
>>
>>      > In the programme below I am trying to read two csv format files and
>>      > process them and write a new file with some of theirs data.
>>      >
>>      > import csv
>>      > f1_reader = csv.reader(open(r"Z:\Weka
>>      > work\Feature_Vectors_Fullset_**00.arff"))
>>      > f2_reader = csv.reader(open(r"Z:\Weka
>>      > work\Feature_Vectors_Fullset_**00_noxy+class.arff"))
>>      > nf = open(r"Z:\Weka work\classified_image00_**withoutxy.arff",
>> "w")
>>      >
>>      > while True:
>>      >     l1 = f1_reader.next()
>>      >     while len(l1) != 12:
>>      >         l1 = f1_reader.next()
>>      >     l2 = f2_reader.next()
>>      >     while len(l2) != 11:
>>      >         l2 = f2_reader.next()
>>      >
>>      >     ix = l1[0].strip()
>>      >     iy = l1[1].strip()
>>      >     classification = l2[8].strip()
>>      >
>>      >     print >> nf, ix, iy, classification
>>      >
>>      > nf.close()
>>      >
>>      > This programme is giving me this error now :
>>      >
>>      > Traceback (most recent call last):
>>      >   File "Z:\Weka work\final_image_**classificationwithoutxy.py",
>>     line 16, in
>>      > <module>
>>      >     l2 = f2_reader.next()
>>      > StopIteration
>>      >
>>      >
>>      > what could be a possible reason to StopIteration ???
>>
>>     next() raises StopIteration when there is nothing else to return.
>>
>>
>>     py> it = iter([1, 2, 3])
>>     py> it.next()
>>     1
>>     py> it.next()
>>     2
>>     py> it.next()
>>     3
>>     py> it.next()
>>     Traceback (most recent call last):
>>        File "<stdin>", line 1, in <module>
>>     StopIteration
>>
>>
>>     You have reached the end of the file and there is nothing else for
>>     the CSV
>>     reader to return, so it raises StopIteration.
>>
>>
>>
>> But why does it has nothing to return so early before traversing the
>> whole file ? Is there any way it can be corrected ?  And also the
>> programme isn't writing anything to the file ?
>>
>>  Try adding some logging so that you can see what it's doing. A simple way
> would be something like:
>
> log_file = open(r"Z:\Weka work\log.txt", "w")
>
> ...
>
> l1 = f1_reader.next()
> print >> log_file, "Read from f1:", l1
> print >> log_file, "Length is", len(l1)
>
> while len(l1) != 12:
>     l1 = f1_reader.next()
>     print >> log_file, "Read from f1:", l1
>     print >> log_file, "Length is", len(l1)
>
> and so on.
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>


Thanks :) This worked !!!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web