在python中如何更改csv中的日期格式? [英] In python how to change date format in a csv?
本文介绍了在python中如何更改csv中的日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用python将日期格式从 30-Jan-02
更改为 30.Jan.2002
在csv文件中的第二位置.我尝试了几件事,但对字符串和字节的兼容性感到困惑.
I am trying to change the format of dates from 30-Jan-02
to 30.Jan.2002
occurring in second position in a csv file using python.
I tried several things but I am confused with strings and bytes comptability.
import os
import csv
from datetime import datetime
import sys
from tempfile import NamedTemporaryFile
with open("Filenamecsv",'r') as csvfile, NamedTemporaryFile(dir="path/parh",delete=False) as temp:
w = csv.writer(temp)
r = csv.reader(csvfile)
for row in r:
dt = row[2].split("-")
row[2] = "{}.{}.{}".format(row[-1],row[1],row[0])
w.writerow(row)
move(temp.name,"Filename.csv")
推荐答案
您还可以使用 datetime
In [25]: import datetime
In [26]: dt = datetime.datetime.strptime("30-Jan-02", '%d-%b-%y').strftime('%d.%b.%Y')
In [27]: dt
Out[27]: '30.Jan.2002'
这篇关于在python中如何更改csv中的日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文