简体   繁体   English

从CSV导入时更改日期格式

[英]Change date format when importing from CSV

I have a CSV file where the date is formatted as yy/mm/dd , but Excel is reading it wrongly as dd/mm/yyyy (eg 8th September 2015 is read as 15th of September 2008). 我有一个CSV文件,其中日期的格式设置为yy/mm/dd ,但是Excel错误地将其读取为dd/mm/yyyy (例如,2015年9月8日为2008年9月15日)。

I know how to change the format that Excel outputs , but how can I change the format it uses to interpret the CSV data? 我知道如何更改Excel 输出的格式,但是如何更改它用于解释CSV数据的格式?

I'd like to keep it to Excel if possible, but I could work with a Python program. 如果可能,我希望将其保留在Excel中,但是我可以使用Python程序。

Option 3. Import it properly 选项3.正确导入

Use DATA, Get External Data, From Text and when the wizard prompts you choose the appropriate DMY combination (Step 3 of 3, Under Column data format, and Date). 使用“数据”,“获取外部数据”,“来自文本”,并在向导提示您选择适当的DMY组合时(第3步,共3步,在“列数据格式”下,以及“日期”)。

Option 1. change the format excel reads in 选项1.更改excel读取的格式

edit: a better method is suggested in the OP comments to accomplish this, I was not aware you could do that 编辑:OP注释中建议使用更好的方法来完成此操作,但我不知道您可以这样做

it(excel) uses your windows settings 它(excel)使用您的Windows设置

so you can go to 所以你可以去

Control Panel > Clock, Language, Region > (under Region and Language) change the date,time or number format 控制面板>时钟,语言,地区>(在地区和语言下)更改日期,时间或数字格式

and enter the appropriate format 并输入适当的格式

Option 2. change the csv date format 选项2.更改CSV日期格式

from dateutil.parser import parse
with open("output.csv","wb") as fout:
    csv_out = csv.writer(fout)
    for row in csv.reader(open("input.csv","rb")):
        row[date_index] = parse(row[date_index]).strftime("%x")
        csv_out.writerow(row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM