编辑:我的道歉:我发现这个问题,我用Rstudio运行代码,这弄乱了一些东西,我只是尝试从控制台,它是正常工作
我知道,我做了一些愚蠢的事情,但我不知道我做错了什么,我建立了这个脚本,读取一个zip文件,做一些转换,并编写一个最终的csv,但出于某种原因,只有最后一个文件得到了写 脚本是完全可复制的,如果您想尝试并调试它,源文件位于下面的链接中。你知道吗
files = os.listdir(os.curdir)
files = [i for i in files if i.endswith('.zip')]
print(files)
for x in files:
path_file = os.path.join(curDir ,x)
print(path_file)
source = pd.read_csv(path_file,
skiprows=1,
usecols=["DISPATCH","1" ,"SETTLEMENTDATE", "RUNNO","INTERVENTION","CASESUBTYPE","SOLUTIONSTATUS","NONPHYSICALLOSSES"],
dtype=str)
source.rename(columns={'1': 'version'}, inplace=True)
source.query('version=="2"')
################ Extract UNIT, SETTLEMENTDATE,DUID,INITIALMW AND EXPORT TO CSV
df_unit=source
df_unit=df_unit.query('DISPATCH=="DUNIT" or DISPATCH=="TUNIT"')
#Make first row a header
df_unit.columns = df_unit.iloc[0]
df_unit = df_unit[1:]
#create a conditional column
df_unit.loc[df_unit['DUNIT'] == 'TUNIT', 'INITIALMW1'] = df_unit['INTERVENTION']
df_unit.loc[df_unit['DUNIT'] == 'DUNIT', 'INITIALMW1'] = df_unit['INITIALMW']
df_unit.drop(columns=['RUNNO','2','INTERVENTION','INITIALMW','DISPATCHMODE'],inplace=True)
df_unit.rename(columns={'INITIALMW1': 'INITIALMW','DUNIT': 'UNIT'}, inplace=True)
df_unit=df_unit.query('SETTLEMENTDATE!="SETTLEMENTDATE" and INITIALMW !="0"')
df_unit["INITIALMW"] = pd.to_numeric(df_unit["INITIALMW"])
df_unit['SETTLEMENTDATE']=pd.to_datetime(df_unit['SETTLEMENTDATE'])
df_unit.head()
df_unit.to_csv(x.rsplit('.', 1)[0] + '.csv',float_format="%.4f",
index=False,date_format='%Y-%m-%dT%H:%M:%S.%fZ',compression='gzip')
print(path_file)
EDIT: I added list files:
['PUBLIC_DAILY_201906040000_20190605040502.zip', 'PUBLIC_DAILY_201906050000_20190606040501.zip', 'PUBLIC_DAILY_201907140000_20190715040502.zip']
The files are downloaded from here.
下面的代码正在为我工作。可能您在当前目录中只有一个下载的zip文件,或者实际上您正在从不正确的目录调用jupyter notebook或python脚本。您可以打印
os.getcwd()
。否则,代码没有问题。所有的zip文件都必须在同一个目录下,您可以从中通过python脚本或jupyter笔记本运行这些代码。你知道吗相关问题 更多 >
编程相关推荐