【问题标题】:Problem importing csv files to pandas to avoid "IndexError: list index out of range"将 csv 文件导入 pandas 以避免“IndexError:列表索引超出范围”时出现问题
【发布时间】:2020-08-29 11:17:08
【问题描述】:

我正在尝试学习 python/pandas。我正在研究“Python 分析基础”,但遇到了困难

使用

input_file = sys.argv[1]

给出结果

文件“C:\Users\longr\Desktop\pfile\1excel_introspect_workbook.py”,第 11 行,在 输入文件 = sys.argv[1] IndexError: 列表索引超出范围

在之前的练习中用

替换这个调用
input_file = 'supplier_data.csv'

works... [对于 csv 文件] 我使用了来自 github 的源代码 - 同样的错误。我所有的文件 [.py / .xlsx / .csv] 都在 C:\Users\longr\Desktop\pfile\ .... 但我不知所措

有人可以帮忙吗?


import sys
from xlrd import open_workbook

input_file = sys.argv[1]

workbook = open_workbook(input_file)
print('Number of worksheets:', workbook.nsheets)
for worksheet in workbook.sheets():
    print("Worksheet name:", worksheet.name, "\tRows:", worksheet.nrows, "t\Columns:", worksheet.ncols)

【问题讨论】:

  • 我不确定,因为我不知道你如何运行你的 scrpt,但 sys.argv 是一个从零开始索引的数组。所以可能你应该像这样运行 python name-of-script.py name-of-file.xlsx。如果你像这样运行它,那么直到看到你编辑的代码才知道
  • 感谢您的 cmets :)。非常感谢您的输入...我需要解决这个问题,以便我可以(最终)使用搜索字符串连接多个文件,以查找具有相似文件名或类型的多个文件

标签: excel pandas csv spyder


【解决方案1】:

sys.argv[1] 是您在 Python 文件名之后为运行脚本而输入的第三个输入。 假设您的 py 脚本名为 example.py,那么您将像运行它一样

python example.py

但如果您想将 csv 文件作为argv[1] 获取,那么您需要像这样运行脚本

python example.py supplier_data.csv

现在你的 argv[0] == example.pyargv[1] == supplier_data.csv 作为字符串类型。

【讨论】:

    【解决方案2】:

    经过一番进一步的寻找,我找到了这个网站 https://www.youtube.com/watch?v=kWaerL6-OiU 这解决了我在多个 Excel 表中阅读时遇到的问题

    #import numpy as np
    import pandas as pd
    import glob
    
    #### Combine, concatenate, join multiple excel files in a given folder into one dataframe, Each excel files having multiple sheets 
    #### All sheets in a single Excel file are first combined into a dataframe, then all the Excel Books in the folder
    #### Are combined to make a single data frame. The combined data frame is the exported into a single Excel sheet.
    
    
    #path = r'C:\Users\Tchamna\Downloads\UTRC_DATA\495GowanusSpeedData20152016'
    path = r'C:\Users\Tchamna\Downloads\UTRC_DATA\test'
    
    filenames = glob.glob(path + "/*.xlsx")
    print(filenames)
    
    ### Dataframe Initialization
    concat_all_sheets_all_files = pd.DataFrame()
    
    
    for file in filenames:
    
            ### Get all the sheets in a single Excel File using  pd.read_excel command, with sheet_name=None
            ### Note that the result is given as an Ordered Dictionary File
            ### Hell can be found here: https://pandas.pydata.org/pandas-docs...
    
            df = pd.read_excel(file, sheet_name=None, skiprows=None,nrows=None,usecols=None,header = 0,index_col=None)
            #df = pd.read_excel(file, sheet_name=None, skiprows=0,nrows=34,usecols=105,header = 9,index_col=None)
    
            #print(df)
    
            ### Use pd.concat command to Concatenate pandas objects as a Single Table.
            concat_all_sheets_single_file = pd.concat(df,sort=False)
    
    
    
             ### Use append command to append/stack the previous concatenated data on top of each other 
            ### as the iteration goes on for every files in the folder
    
            concat_all_sheets_all_files=concat_all_sheets_all_files.append(concat_all_sheets_single_file)
            #print(concat_all_sheets)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2018-05-15
      • 2017-09-09
      • 2011-10-31
      • 2015-06-26
      相关资源
      最近更新 更多