我必须为报告构建自动导入。这些在csv文件和excel文件之间可能会有所不同。我得到了读取和拆分csv(需要拆分和重新排列日期)的部分。当我尝试从pycharm中读取Excel时,我的问题开始了。所有这些都可以在jupyter笔记本中使用,但是似乎在pycharms中遇到错误(由于缩进和填充,我已经知道无法将jn的内容复制到pych。) 这是我的代码,我想知道索引在何处以及为什么超出范围。日期为“ yyyy / mm / dd”
datum = []
try:
if df.columns[0] == "Day":
for line in df["Day"]:
date_emer = line.split("/")
date_new = [date_emer[2], date_emer[1], date_emer[0]]
date_new_join = "".join(date_new)
datum.append(int(date_new_join))
date_correct = datetime(year=int(date_new_join[0:4]), day=int(date_new_join[4:6]), month=int(date_new_join[6:8]))
df["Day"] = date_correct
print(df)
elif df.columns[0] == "day":
for line in df["day"]:
date_emer = line.split("/")
date_new = [date_emer[0], date_emer[1], date_emer[2]]
date_new_join = "".join(date_new)
datum.append(int(date_new_join))
date_correct = datetime(year=int(date_new_join[0:4]), month=int(date_new_join[4:6]), day=int(date_new_join[6:8]))
df["day"] = date_correct
print(df)
else:
print("Unknown Column! Error!")
except Exception as e:
print("Error Message: " +str(e))
这是回溯:
Traceback (most recent call last):
File "sales_import_outbrain.py", line 99, in <module>
raise e
File "sales_import_outbrain.py", line 89, in <module>
date_new = [date_emer[0], date_emer[1], date_emer[2]]
IndexError: list index out of range
答案 0 :(得分:2)
year,month,day = line.split("/")
date_correct = datetime(year=int(year), day=int(day), month=int(month))
我认为您可以直接使用