Python Forum
How to read filename and specific cell value from multiple csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to read filename and specific cell value from multiple csv
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Color a table cell based on specific text Creepy 11 2,008 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  Rename part of filename in multiple files atomxkai 7 7,375 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Open and read multiple text files and match words kozaizsvemira 3 6,764 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  [Solved] Trying to read specific lines from a file Laplace12 7 3,558 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  How to append a value to specific excel cell using openpyxl hobbyist 0 4,861 Mar-05-2021, 07:14 PM
Last Post: hobbyist
  read logfile between two specific strings FelixReiter 6 2,940 Jan-04-2021, 02:26 PM
Last Post: FelixReiter
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 4,215 Jul-03-2020, 01:34 AM
Last Post: bowlofred
  Read Multiples Text Files get specific lines based criteria zinho 5 3,138 May-19-2020, 12:30 PM
Last Post: zinho
  How to read multiple csv files and merge data rajeshE 0 1,955 Mar-28-2020, 04:01 PM
Last Post: rajeshE
  KeyError -read multiple lines bongielondy 2 3,457 Nov-06-2019, 01:33 AM
Last Post: bongielondy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020