PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

[FIXED] how to read multiple csv files in a directory through python csv() function?

 August 29, 2022     csv, python     No comments   

Issue

In one of my directory, I have multiple CSV files. I wanted to read the content of all the CSV file through a python code and print the data but till now I am not able to do so.

All the CSV files have the same number of columns and the same column names as well.

I know a way to list all the CSV files in the directory and iterate over them through "os" module and "for" loop.

for files in os.listdir("C:\\Users\\AmiteshSahay\\Desktop\\test_csv"):

Now use the "csv" module to read the files name

reader = csv.reader(files)

till here I expect the output to be the names of the CSV files. which happens to be sorted. for example, names are 1.csv, 2.csv so on. But the output is as below

<_csv.reader object at 0x0000019F97E0E730>
<_csv.reader object at 0x0000019F97E0E528>
<_csv.reader object at 0x0000019F97E0E730>
<_csv.reader object at 0x0000019F97E0E528>
<_csv.reader object at 0x0000019F97E0E730>
<_csv.reader object at 0x0000019F97E0E528>

if I add next() function after the csv.reader(), I get below output

['1']
['2']
['3']
['4']
['5']
['6']

This happens to be the initials of my CSV files name. Which is partially correct but not fully.

Apart from this once I have the files iterated, how to see the contents of the CSV files on the screen? Today I have 6 files. Later on, I could have 100 files. So, it's not possible to use the file handling method in my scenario.

Any suggestions?


Solution

I would recommend reading your CSVs using the pandas library. Check this answer here: Import multiple csv files into pandas and concatenate into one DataFrame

Although you asked for python in general, pandas does a great job at data I/O and would help you here in my opinion.



Answered By - louis_guitton
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing