Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 循环浏览和转换excel文件时无法分配给函数调用_Python - Fatal编程技术网

Python 循环浏览和转换excel文件时无法分配给函数调用

Python 循环浏览和转换excel文件时无法分配给函数调用,python,Python,使用此代码: xls = pd.ExcelFile('test.xlsx') sn = xls.sheet_names for i,snlist in list(zip(range(1,13),sn)): 'df{}'.format(str(i)) = pd.read_excel('test.xlsx',sheet_name=snlist, skiprows=range(6)) 我得到这个错误: 格式(str(i))=pd.read\u excel('

使用此代码:

    xls = pd.ExcelFile('test.xlsx')
    sn = xls.sheet_names
    for i,snlist in list(zip(range(1,13),sn)):
        'df{}'.format(str(i)) =  pd.read_excel('test.xlsx',sheet_name=snlist, skiprows=range(6))
我得到这个错误:

格式(str(i))=pd.read\u excel('test.xlsx',sheet\u name=snlist, skiprows=射程(6)) ^SyntaxError:无法分配给函数调用

我无法理解错误以及如何解决。有什么问题吗?
df+str(i)也返回错误

我想得出如下结果: df1=局部放电读取excel。。清单1。。。
df2=pd.read\u excel。。。列表2….

您不能将
df.read\u excel
的结果分配给
'df{}.format(str(i))
——这是一个看起来像
“df0”
“df1”
“df2”
等的字符串。这就是您收到此错误消息的原因。错误消息可能令人困惑,因为它将其视为“函数调用”的赋值

似乎您需要一个
DataFrame
s的列表或字典

为此,请将
df.read_excel
的结果分配给变量,例如
df
,然后
将其附加到列表中,或将其添加到
数据框的字典中

列表如下:

dataframes = []
xls = pd.ExcelFile('test.xlsx')
sn = xls.sheet_names
for i, snlist in list(zip(range(1, 13), sn)):
    df = pd.read_excel('test.xlsx', sheet_name=snlist, skiprows=range(6))
    dataframes.append(df)
作为字典:

dataframes = {}
xls = pd.ExcelFile('test.xlsx')
sn = xls.sheet_names
for i, snlist in list(zip(range(1, 13), sn)):
    df = pd.read_excel('test.xlsx', sheet_name=snlist, skiprows=range(6))
    dataframes[i] = df
在这两种情况下,您都可以通过如下索引方式访问数据帧:

for i in range(len(dataframes)):
    print(dataframes[i])
# Note indexes will start at 0 here instead of 1
# You may want to change your `range` above to start at 0
或者更简单地说:

for df in dataframes:
    print(df)
就字典而言,您可能需要:

for i, df in dataframes.items():
    print(i, df)
# Here, `i` is the key and `df` is the actual DataFrame
如果您确实希望将
df1
df2
等作为键,请执行以下操作:

dataframes[f'df{i}'] = df

错误信息非常清楚<代码>=
是赋值,不能为函数调用赋值。不知道你对这件事有什么不了解。
'df{}.format(str(i))=pd.read\u excel('test.xlsx',sheet\u name=snlist,skiprows=range(6))
应该做什么?您缺少一些Python数据类型的基础知识。您试图将函数调用的结果分配给临时字符串。这是矛盾,;这是尝试更改常量的简单版本,例如
“hello”=“a”+“b”
。。df1=局部放电读取excel,。。,。。清单1。。df2=pd.read\u excel。。。清单2。。。。df+str(i)也返回错误@신동희 请参阅下面可能的解决方案——使用dict或列表