如何使用python从单个csv文件中的多个工作表读取数据 [英] how to read data from multiple sheets in a single csv file using python

查看:5056
本文介绍了如何使用python从单个csv文件中的多个工作表读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python,我有一个csv文件与数据在多个表。

I am using python and i had a csv file with data in multiple sheets.

因此,我们可以使用python从csv文件读取数据

So we can read data from csv file using python as below

假设 sheet1 中的 data.csv 文件中的数据低于

Suppose the data in data.csv file in sheet1 is below

what    |   are   |  you   | doing | however
hello   |   that  |  would |   be  | useful

csv_file = "/home/user/csv_folder/data.csv" 

for line in open(csv_file,'r'):
    print line
    ..........
    ..........

上面的输出将如下

"what are you doing however"
"hello that would be useful"

但在同一个 data.csv 文件,我有另一个 sheet2 的数据如下

but in the same data.csv file, i had data in another sheet2 as below

This    |   will   |  be   |   second  | sheet | data
That    |   would  |  lot  |   useful  | now

现在我想做的是打印另一个 sheets (如果单个csv文件中存在超过1个工作表)。

Now what i am trying to do is to print the data which is present in another sheets(more than 1 sheets if present in a single csv file).

任何人都可以让我们如何打印所有工作表中的数据都存在于单个 csv 文件中?

Can anyone let me now how we can print the data in all the sheets present in a single csv file ?

任何人都可以分享一些python代码在单个csv文件中处理多个工作表?

Can anyone please share a bit of python code that process more than one sheet present in a single csv file ?

推荐答案

csv文件中没有的评论者已正确指出。

There is no concept of "sheets" in csv file like many of the commentators have pointed out correctly.

如果您尝试从excel文件中读取工作表,这里是一个很好的博客资源,您可以找出语法。

If you are trying to read "sheets" from an excel file, here is a good blog resource for you to figure out the syntax.

http://michalisavraam.org/2009/06/manipulating-excel-files-using-python-part-1-reading-files/

请注意,如果你确实打算从excel文件中读取sheets,上述博客链接 xlrd 需要的第三方包以单独下载并安装在您的系统中。像这样: -

Do note that if you are indeed intending to read "sheets" from an excel file, the 3rd party package recommended by the above blog link xlrd needs to be separately downloaded and installed in your system. Like this:-

pip install xlrd

之前,您可以按照博客文章的建议,在您的python脚本中 import xlrd

before you can import xlrd in your python script as recommended by the blog post.

这篇关于如何使用python从单个csv文件中的多个工作表读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆