我是靠谱客的博主 幽默冬瓜,最近开发中收集的这篇文章主要介绍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 csv()函数读取目录中的多个csv文件?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部