我是靠谱客的博主 幽默冬瓜,这篇文章主要介绍python读取多个csv文件-如何通过python csv()函数读取目录中的多个csv文件?,现在分享给大家,希望可以做个参考。

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:UsersAmiteshSahayDesktoptest_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?

解决方案

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.

最后

以上就是幽默冬瓜最近收集整理的关于python读取多个csv文件-如何通过python csv()函数读取目录中的多个csv文件?的全部内容,更多相关python读取多个csv文件-如何通过python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(119)

评论列表共有 0 条评论

立即
投稿
返回
顶部