Python Forum

Full Version: How to read filename and specific cell value from multiple csv
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi,

i have muliple csv's , i am writing a module that can read multiple csv's and take one cell position and pick it's value and append it to new csv file with the file name:

till now i have reached here:

import csv
import glob
import os

finaldata = []

listcsv = glob.glob("*.csv")
for i in listcsv:
    filename = os.path.split(i)# here read csv
    finaldata.append(filename)
    with open(i, "rb") as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            Rnum = row[0]
            cell2 = row[1][4]# cell position is b5
            filedata.append(cell2)

filename = "Rahu_ketu.csv"
with open(filename, "w")as csvfile:
    csv = csv.writer(csvfile)
    csv.writerow("")
    for i in range(0, len(filedata)):
        csvfile.writerow(filedata[i])
whenever i run this code it says

Error:
Traceback (most recent call last): File "C:\Users\prince.bhatia\Desktop\task_500\task_500.py", line 15, in <module> cell2 = row[1][4]# cell position is b5 IndexError: string index out of range
can anyone tell me how can i pick that specific cell position and can also take file name and append it to new csv...every csv has same cell position which needed to be picked