我想改變日期的格式從30-Jan-02
到30.Jan.2002
發生在使用python的csv文件的第二個位置。 我嘗試了幾件事情,但我很困惑與字符串和字節compability。在python中如何更改csv中的日期格式?
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")
什麼是你的代碼確切的問題?你有錯誤信息嗎? – MrLeeh
您面臨的問題是什麼? – MONTYHS
如果你的日期在第二個位置,它應該在''row [1]''中。如果它的格式是''30-Jan-02'',那你爲什麼要用''/''來分割?當你建立新的日期時,你想在'''{}。{}。{}。。format(...)''中使用''dt''而不是''row''。 –