I am trying to find few items from a CSV file when I run the code sometimes it works but sometimes it produces error list index out of range
def find_check_in(name,date):
x = 0
f = open('employee.csv','r')
reader = csv.reader(f, delimiter=',')
for row in reader:
id = row[0]
dt = row[1]
v = row[2]
a = datetime.strptime(dt,"%Y-%m-%d")
if v == "Check-In" and id=="person":
x = 1
f.close()
return x
Traceback (most recent call last):
File "", line 51, in
x=find_check_in(name,date)
File "", line 21, in find_check_in
id = row[0]
IndexError: list index out of range
Your CSV file contains blank lines, resulting in row becoming an empty list, in which case there is no index 0, hence the error. Make sure your input CSV has no blank line, or add a condition to process the row only if it isn't empty:
for row in reader:
if row:
# the rest of your code
Seems like reader is returning a row with no elements. Does your data contain any such rows? Or perhaps you need to use the newline='' argument to reader?
https://docs.python.org/3/library/csv.html#csv.reader
Related
Following is my input csv file contents
file3.csv:
a,ab
b,cd
c,nav
d,test
name,port
I want to write this into a existing csv file, in a specific column numbers.
For example:
I want to write, a,b,c,d,name into a column number --- AA
And I need to write ab,cd,nav,test,port into a column number ---AB
Python Script:
import csv
f1 = open ("file3.csv","r") # open input file for reading
with open('file4.csv', 'wb') as f: # output csv file
writer = csv.writer(f)
with open('file3.csv','r') as csvfile: # input csv file
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
row[7] = f1.readline() # edit the 8th column
writer.writerow(row)
f1.close()
I am getting following error:
MacBook-Pro:test$ python three.py
Traceback (most recent call last):
File "three.py", line 10, in
row[7] = f1.readline() # edit the 8th column
IndexError: list assignment index out of range
You can not use an index into a list for an element that does not already exist. You will need to increase the length of the row before assigning elements to specific indices.
If you want to assign to row[7] try this before:
if len(row) < 8:
row += [None] * (8 - len(row))
So, your inner loop will likely need to look something like:
for row in reader:
if len(row) < 8:
row += [None] * (8 - len(row))
new_values = f1.readline().strip().split(',')
row[7:7+1+len(new_values)] = new_values
writer.writerow(row)
I want to read only first column from csv file. I tried the below code but didn't got the result from available solution.
data = open('data.csv')
reader = csv.reader(data)
interestingrows = [i[1] for i in reader]'
The error I got is:
Traceback (most recent call last):
File "G:/Setups/Python/pnn-3.py", line 12, in <module>
interestingrows = [i[1] for i in reader]
File "G:/Setups/Python/pnn-3.py", line 12, in <listcomp>
interestingrows = [i[1] for i in reader]
IndexError: list index out of range
You can also use DictReader to access columns by their header
For example: If you had a file called "stackoverflow.csv" with the headers ("Oopsy", "Daisy", "Rough", and "Tumble")
You could access the first column with this script:
import csv
with open(stackoverflow.csv) as csvFile:
#Works if the file is in the same folder,
# Otherwise include the full path
reader = csv.DictReader(csvFile)
for row in reader:
print(row["Oopsy"])
If you want the first item from an indexable iterable you should use 0 as the index. But in this case you can simply use zip() in order to get an iterator of columns and since the csv.reader returns an iterator you can use next() to get the first column.
with open('data.csv') as data:
reader = csv.reader(data)
first_column = next(zip(*reader))
I am 99% of the way there...
def xl_to_csv(xl_file):
wb = xlrd.open_workbook(xl_file)
sh = wb.sheet_by_index(0)
output = 'output.csv'
op = open(output, 'wb')
wr = csv.writer(op, quoting=csv.QUOTE_ALL)
for rownum in range(sh.nrows):
part_number = sh.cell(rownum,1)
#wr.writerow(sh.row_values(rownum)) #writes entire row
wr.writerow(part_number)
op.close()
using wr.writerow(sh.row_values(rownum)) I can write the entire row from the Excel file to a CSV, but there are like 150 columns and I only want one of them. So, I'm grabbing the one column that I want using part_number = sh.cell(rownum,1), but I can't seem to get the syntax correct to just write this variable out to a CSV file.
Here's the traceback:
Traceback (most recent call last):
File "test.py", line 61, in <module>
xl_to_csv(latest_file)
File "test.py", line 32, in xl_to_csv
wr.writerow(part_number)
_csv.Error: sequence expected
Try this:
wr.writerow([part_number.value])
The argument must be a list-like object.
The quickest fix is to throw your partnum in a list (and as per Abdou you need to add .value to get the value out of a cell):
for rownum in range(sh.nrows):
part_number = sh.cell(rownum,1).value # added '.value' to get value from cell
wr.writerow([part_number]) # added brackets to give writerow the list it wants
More generally, you can use a list comprehension to grab the columns you want:
cols = [1, 8, 110]
for rownum in range(sh.nrows):
wr.writerow([sh.cell(rownum, colnum).value for colnum in cols])
I am having some problems writing back the values that I want to the output csv file. My intention is to get the values of the csv file and change those values that are equal to 'Never-worked' or 'Without-pay' to being 'Not-working'. My code successfully fulfills this, however, it does not write it back to the csv file appropriately, meaning the changed values are not written back, but the file remains the same as the original. What am I doing wrong?
import csv
infile = open('income.csv','rb')
outfile = open('income-new.csv', 'wb')
def changeOccupation(cell):
if (cell.lower() == 'never-worked' or cell.lower() == 'without-pay'):
cell = 'Not-working'
print(cell)
#go trough each line of the file
for line in infile:
row = line.split(',')
for cell in row:
changeOccupation(cell)
#print(row)
outfile.write(','.join(row))
infile.close()
outfile.close()
You need to retun the new value and wright the changed row pack:
def changeOccupation(cell):
if (cell.lower() == 'never-worked' or cell.lower() == 'without-pay'):
return 'Not-working'
return cell
#go trough each line of the file
for line in infile:
row = line.split(',')
new_row = [changeOccupation(cell) for cell in row]
outfile.write(','.join(new_row))
I am new to python but have experience with C/sh/java..
My first homework is csv field replacement, if field has specific value
My csv file has 24 columns and has ~ as field seperator.
I want to change field 24th with T if it's True
My code is like that
import csv
with open ('MyCustomerList.csv', 'r', encoding='utf-8' ) as fi:
reader = csv.reader (fi, delimiter = '~')
for row in reader:
if row[23] == 'True':
print (row[23])
But it gives an error like that
C:\>c:\Python34\python.exe pyt1.py
Traceback (most recent call last):
File "pyt1.py", line 5, in <module>
if row[23] == 'True':
IndexError: list index out of range
I could not figured out the issue
what is the error ?
The error 'list index out of range' says that you tried to get element after the array ends. I assume that your file really has 24 values, so at first try printing all of them out with index:
i = 0
for row in reader:
print (i + ' ' + row)
i += 1
and check if you really get 23rd field (indexing from 0 :).