Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

PYTHON > Rearrange item data ("date") within .csv file

Source csv:

column a, column b, 2021-01-09, column d
column a, column b, 2021-01-10, column d
column a, column b, 2021-01-11, column d

Desired output:

09/01/2021, column a, column b, column d
10/01/2021, column a, column b, column d
11/01/2021, column a, column b, column d

What is the best method to achieve this, pythonically?
Thanks in advance!

question from:https://stackoverflow.com/questions/65643147/python-rearrange-item-data-date-within-csv-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assuming your row from the csv is like

row = ["column a", "column b", "2021-01-09", "column d"]

you can do something like

import datetime

row.sort()
row[0] = datetime.datetime.strptime(row[0], '%Y-%m-%d').strftime('%d/%m/%y')

gives row as

>>> row
['09/01/21', 'column a', 'column b', 'column d']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...