473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

CSV: list index out of range error

How do I check if a certain column exists in the CSV file, from the following code I try to check if s[8] exists. It gives me an error INDEX ERROR: List index out of range. If its Null I expect it to print NO rather than give me an error


Expand|Select|Wrap|Line Numbers
  1. f = open('C:\\Documents and Settings\\adesai\\Desktop\\save_lines.csv','r')
  2.  
  3.         while f:
  4.             line = f.readline()
  5.             if not line:
  6.                 break
  7.  
  8.             s = line.split(',')
  9.  
  10.  
  11.             if s[8]:
  12.                     print "YES"
  13.             else:
  14.                     print "NO"        
Nov 21 '07 #1
1 4888
bvdet
2,851 Expert Mod 2GB
How do I check if a certain column exists in the CSV file, from the following code I try to check if s[8] exists. It gives me an error INDEX ERROR: List index out of range. If its Null I expect it to print NO rather than give me an error


Expand|Select|Wrap|Line Numbers
  1. f = open('C:\\Documents and Settings\\adesai\\Desktop\\save_lines.csv','r')
  2.  
  3.         while f:
  4.             line = f.readline()
  5.             if not line:
  6.                 break
  7.  
  8.             s = line.split(',')
  9.  
  10.  
  11.             if s[8]:
  12.                     print "YES"
  13.             else:
  14.                     print "NO"        
To avoid the IndexError exception, you can check for s[8] in a try/except block:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     if s[8]:
  3.         print "Yes"
  4. except IndexError, e:
  5.     print "No"
Nov 22 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Dave [Hawk-Systems] | last post by:
have the table "numbercheck" Attribute | Type | Modifier -----------+------------+---------- svcnumber | integer | not null svcqual | varchar(9) | svcequip | char(1) | svctroub ...
1
by: tkpmep | last post by:
I expected the following code to work: f = file(fn,"wb") writer = csv.writer(f) for i in range(IMax): writer.writerow(].append( for j in range(N)])) but instead i got the following error...
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
0
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
22
by: fniles | last post by:
I have a .CSV file (comma delimited) that I want to open using OLEDB, but I get the error "External table is not in the expected format." If I save the .CSV file to an .XLS file, I can open the...
10
by: CarpeSkium | last post by:
How do I access the value in the second row in the first position of a CSV? Or the 3rd row, in the fifth position? a,b,c,d,e,f,g,h,i j,k,l,m,n,o,p,q,r r,s,t,v,w,x,y,z I'd want to get at "j"...
5
by: newsaboutgod | last post by:
I think VB.NET drives some people crazy because some simple VB6 things seem so hard. Here is some VB6 code: 'Write CSV File open "c:\test.csv" for output as #1 write#1, "1","2","3","4","5"...
6
by: Shawn Minisall | last post by:
I'm writing a game that uses two functions to check and see if a file called highScoresList.txt exists in the main dir of the game program. If it doesn, it creates one. That part is working fine. ...
2
by: Georgy Panterov | last post by:
I am a relatively new python user. I am writing an economic simulation of acard-game. The simulation runs fine for a few iteration but then it gives an error of "list index out of range." The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.