简体   繁体   English

如何通过将工作表名称作为参数传递,使用 pandas 库的 read_csv() function 从 CSV 文件读取特定工作表?

[英]how read a specific sheet from a CSV file using read_csv() function of pandas library by passing sheet name as an argument?

i want to read a csv file with the file name, when i pass the sheet name as an argument i am getting an error message.我想读取带有文件名的 csv 文件,当我将工作表名称作为参数传递时,我收到一条错误消息。

i tried the following code and it did not work.我尝试了以下代码,但没有用。 import pandas as pd df = pd.read_csv('file_name.csv',sheet_name='sheet 1',header = 1) The error message is " read_csv() got an unexpected keyword argument 'sheet_name' " import pandas as pd df = pd.read_csv('file_name.csv',sheet_name='sheet 1',header = 1)错误信息是“read_csv() got an unexpected keyword argument 'sheet_name'”

I think you should use excel_parse instead of parse_CSV, because CSV is a comma separated text file, which does not contain multiple sheets.我认为您应该使用 excel_parse 而不是 parse_CSV,因为 CSV 是一个逗号分隔的文本文件,不包含多个工作表。

xls = pd.ExcelFile('path_to_file.xls')
sheet1 = xls.parse(0)

# above will give you first sheet

sheet1 = xls.parse(1)

#This will give you second sheet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM