【问题标题】:pandas OSError: [Errno 22] Invalid argument in read_excel [duplicate]pandas OSError:[Errno 22] read_excel 中的参数无效 [重复]
【发布时间】:2021-09-22 03:48:32
【问题描述】:

我尝试使用 pandas 读取 excel 文件,代码如下:

path = "QVI_transaction_data.xlsx"

我也尝试使用“./QVI_transaction_data.xlsx”而不是上面的那个,名称只是从 os.listdir() 复制粘贴,所以没有转录问题

pd.read_excel(path, sheet_name = "in")

但它没有工作,它输出这个错误:

OSError: [Errno 22] Invalid argument

我也尝试不使用 sheet_name 参数,其他帖子说文件名有问题,但我之前使用过 pandas,我认为名称没有问题。有人知道这有什么问题吗?

文件是这样的:

【问题讨论】:

  • 您能否发布path = "QVI_transaction_data.xlsx" 的显式路径,比如它可能有path = "\home\QVI_transaction_data.xlsx" path = "/home/QVI_transaction_data.xlsx" ,或者如果您能告诉我们您是如何从目录路径读取文件的更好?
  • 我正在使用 colab,这是路径,/content/QVI_transaction_data.xlsx" 我也尝试在我的电脑上使用旧的 python 笔记本,但出现同样的错误
  • 使用完整路径我也得到同样的错误
  • 您是否有想要阅读的特定工作表名称?如果是这样,请确保您使用的是正确的名称。
  • 是的,工作表名是“in”,我用过

标签: python pandas xlsx


【解决方案1】:

由于我们无法复制数据,因此可能会有不同的情况和不同的解决方案。

我列出了一些可能会引导你走向正确方向的情况...

情况一:

如果您使用的是旧的 python 版本,那么您应该简单地尝试下面的sheetname 与旧版本和sheet_name 与新版本。

import pandas as pd
df = pd.read_excel(file_with_data, sheetname=sheet_with_data)

您可以改用pd.ExcelFile ..

xls = pd.ExcelFile('path_to_file.xls')
df1 = pd.read_excel(xls, 'in')

xl = pd.ExcelFile(path)
# xl = pd.ExcelFile("Full_Path_of _file")

xl.sheet_names
[u'in', u'in1', u'in2']
df = xl.parse("in")
df.head()

df = pd.read_excel(open('your_xls_xlsx_filename','rb'), sheet_name='Sheet 1')
# or using sheet index starting 0
df = pd.read_excel(open('your_xls_xlsx_filename','rb'), sheet_name=1)

注意:选择 sheetname 参数需要仔细选择 python pandas 版本>

对于较旧的python版本:使用sheetname

对于新的python版本:使用sheet_name

情况2:

直接从右键文件属性复制地址-安全会导致这个问题所以复制粘贴文件路径也会产生这些问题而没有其他明显问题

解决

It has nothing to do with the backslash /forward slash in the path, and it has nothing to do with whether the path contains. There are two solutions

1 Enter the path manually
2 Open this path in Explorer, then copy

一般阅读约定:

# When the parameter is None, all tables are returned, which is a dictionary of tables;
sheet = pd.read_excel('example.xls',sheet_name= None)
# When the parameter is list = [0, 1, 2, 3], the returned multi-table is also a dictionary
sheet = pd.read_excel('example.xls',sheet_name= 0)
sheet = pd.read_excel('example.xls',sheet_name= [0,1])
#The data of the table can also be read according to the name of the table header or the position of the table
sheet = pd.read_excel('example.xls',sheet_name= 'Sheet0')
sheet = pd.read_excel('example.xls',sheet_name= ['Sheet0','Sheet1'])
sheet = pd.read_excel('example.xls',sheet_name=[0,1,'Sheet3'])

【讨论】:

  • 当 OP 不清楚时,请他们澄清,如果他们仍然不提供,请关闭问题。比猜测多种原因的答案要好。这与路径无关。
  • @smci,同意.. 会记录下来。
【解决方案2】:

我尝试了 csv 和 excel 两种可能的方式。尝试这样的事情:-

import pandas as pds
file =('path_of_excel_file')
newData = pds.read_excel(file)
newData

【讨论】:

  • @L Prathyusha ,如果 xlrdopenpyxl 在系统上运行较旧,则读取 Excel 文件可能会出现问题。
  • 另一种可能是像这样读取文件:-Data=pd.read_csv("File Name..."r) 通过在代码末尾添加“r”可能会有所帮助
【解决方案3】:

可以做的一个可能的事情是将 excel 文件 (.xlsx) 文件转换为 .csv 文件,这可以通过文件完成并使用 csv 文件导出,然后可以像这样加载它:-

import pandas as pd
Data=pd.read_csv("File Name...")
print(Data)

或者,如果您只想直接加载 excel 文件,可以这样做:-

import pandas as pds
file =('path_of_excel_file')
newData = pds.read_excel(file)
newData

【讨论】:

  • 更改文件扩展名也不起作用,read_excel 是我正在使用的功能,但我也尝试使用 ExcelFile
  • 我尝试使用您的文件以获得更多说明,我能够以 excel 和 csv 的形式运行
  • 要阅读 excel 试试这个:-
  • 确保在读取 excel 文件时尝试导入(将 pandas 作为 pds 导入)而不是 pd,如果您将 pandas 作为 pds 导入,它将起作用
  • 并确保将 \ 更改为 /
猜你喜欢
  • 2019-04-27
  • 1970-01-01
  • 2018-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-18
  • 2017-04-10
  • 2021-02-10
相关资源
最近更新 更多