Problem importing csv files to pandas to avoid "IndexError: list index out of range"
Problem importing csv files to pandas to avoid "IndexError: list index out of range"
我正在努力学习 python / pandas。我正在研究“Python 的分析基础”,但遇到了瓶颈
使用
input_file = sys.argv[1]
给出结果
File "C:\Users\longr\Desktop\pfileexcel_introspect_workbook.py", line 11, in
input_file = sys.argv[1]
IndexError: list index out of range
在以前的练习中用
替换此调用
input_file = 'supplier_data.csv'
有效... [对于 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)
sys.argv[1]
是您在 python 文件名后输入到 运行 脚本的第三个输入。
假设你的 py 脚本被命名为 example.py
那么你将 运行 它就像
python example.py
但是如果你想获取 csv 文件 argv[1]
那么你需要 运行 你的脚本
python example.py supplier_data.csv
现在你的
argv[0] == example.py
和
argv[1] == supplier_data.csv
为字符串类型。
经过进一步搜索,我找到了这个网站
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_DATA5GowanusSpeedData20152016'
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)
我正在努力学习 python / pandas。我正在研究“Python 的分析基础”,但遇到了瓶颈
使用
input_file = sys.argv[1]
给出结果
File "C:\Users\longr\Desktop\pfileexcel_introspect_workbook.py", line 11, in input_file = sys.argv[1] IndexError: list index out of range
在以前的练习中用
替换此调用input_file = 'supplier_data.csv'
有效... [对于 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)
sys.argv[1]
是您在 python 文件名后输入到 运行 脚本的第三个输入。
假设你的 py 脚本被命名为 example.py
那么你将 运行 它就像
python example.py
但是如果你想获取 csv 文件 argv[1]
那么你需要 运行 你的脚本
python example.py supplier_data.csv
现在你的
argv[0] == example.py
和
argv[1] == supplier_data.csv
为字符串类型。
经过进一步搜索,我找到了这个网站 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_DATA5GowanusSpeedData20152016'
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)