473,603 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading csv problem

Hello,

I use csv to take information from file.
import csv
reader = csv.reader(open ('t.csv'))

for row in reader:
print row # it is perfectly OK

---------------------------------------------------------------------
But If I use this code I have problem
import csv
reader = csv.reader(open ('t.csv'))

for row in reader:
print row # it is perfectly OK
for row in reader:
print row # it is not printed on the monitor???

Why does only the first print row work here?

TIA,
ajikoe

Nov 15 '06 #1
2 1128
aj****@gmail.co m wrote:
Hello,

I use csv to take information from file.
import csv
reader = csv.reader(open ('t.csv'))

for row in reader:
print row # it is perfectly OK

---------------------------------------------------------------------
But If I use this code I have problem
import csv
reader = csv.reader(open ('t.csv'))

for row in reader:
print row # it is perfectly OK
for row in reader:
print row # it is not printed on the monitor???

Why does only the first print row work here?

TIA,
ajikoe
Because reader is an iterator and has no means of going back to its
beginning.

You could make a list out of it:
lines = list(csv.reader (open('t.csv')) )
for row in lines:
print row
for row in lines:
print row

Hope it helps,

Cameron.
Nov 15 '06 #2
Thank you.
On Nov 15, 1:33 pm, Cameron Walsh <cameron.wa...@ gmail.comwrote:
aji...@gmail.co m wrote:
Hello,
I use csv to take information from file.
import csv
reader = csv.reader(open ('t.csv'))
for row in reader:
print row # it is perfectly OK
---------------------------------------------------------------------
But If I use this code I have problem
import csv
reader = csv.reader(open ('t.csv'))
for row in reader:
print row # it is perfectly OK
for row in reader:
print row # it is not printed on the monitor???
Why does only the first print row work here?
TIA,
ajikoeBecause reader is an iterator and has no means of going back to its
beginning.

You could make a list out of it:
lines = list(csv.reader (open('t.csv')) )
for row in lines:
print row
for row in lines:
print row

Hope it helps,

Cameron.
Nov 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3018
by: Dariusz | last post by:
Below is part of a code I have for a database. While the database table is created correctly (if it doesn't exist), and data is input correctly into the database when executed, I have a problem when reading out the data into the PHP variables / array. It should be displaying the information out in the following way, using the PHP array: n_date, n_headline, n_summarytext, n_fulltext
1
7041
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the file... Thank you to all of you who can help me with this one...
8
18223
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a "char" with an ASCII code in 0..127 I've searched the ANSI C++ standard, the internet and various text books, but can't see how to workaround this one. I've tried wchar_t and wstring without success. But rather than spending lots of time on...
8
5219
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and writing into a dynamic array. My problem is that the array shows extra z and probably because of this further processing gives run time error in borland compiler. Can you please tell me, if the problem is related to handling end-of line , how do i do...
2
2473
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { OperatingSystem os = Environment.OSVersion; AppDomain ad = Thread.GetDomain();
1
2010
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like: mdyn"comment~""comment~"\n Now, I wrote some code to read these records, and it works perfectly for every date I've tried it on, except when the day is 26. I tried saving a record for 6/26/2004 and 7/26/2004 and it read it in as the day, year, and number of...
7
6052
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
5
8932
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column headers). I used ODBC in my VB.NET program to read that spreadsheet into a dataset, to make it easy to manipulate. The code I use to read it is as the bottom of this posting.
5
14977
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
6
3520
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features to an old program that I wrote in delphi and it's a good opportunity to start with c++.
0
7996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8060
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6735
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3951
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.