Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Error in reading and writing CSV format file in python

Reply
Thread Tools

Re: Error in reading and writing CSV format file in python

 
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-11-2013
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

 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      02-11-2013
On 2013-02-11 12:44, inshu chauhan wrote:
>
> On Mon, Feb 11, 2013 at 1:26 PM, Steven D'Aprano
> <(E-Mail Removed)
> <(E-Mail Removed)>> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Error in reading and writing CSV format file in python Dave Angel Python 0 02-11-2013 02:22 PM
Re: Error in reading and writing CSV format file in python MRAB Python 0 02-11-2013 12:27 PM
read and write csv file using csv module jliu66 Python 0 10-19-2007 03:12 PM
How to move data from a CSV file to a JTable, and from a JTable to a CSV file ? Tintin92 Java 1 02-14-2007 06:51 PM
reading/writing Excel formats (or CSV) Phil Tomson Ruby 11 04-06-2005 12:29 PM



Advertisments