The built-in python modules should be sufficient. Avoid premature optimization and only look for ways to improve speed and efficiency if the simple solutions turn out to be too slow.
from datetime import datetime
line = "1,2011,54,0,.375,-.183,2.325,1.221,0,.016,0,0,431.4,.345,1.563,25.13,13.23"
# split string and ignore 4th and every thing from the 6th element on
id, year, julian, _, value, *_ = line.split(",")
# format date, convert julian day-of-year to 'day-month'
date = "%s-%s 00:00:00" % (int(year), datetime.strptime(julian, "%j").strftime("%d-%m"))
print(",".join((date, id, value)))
#>>> 2011-23-02 00:00:00,1,.375
# could cast to numeric types if needed
#id = int(id)
#value = float(value)
Leap years in real Julian dates are not handled easily, especially when looking at the Wiki article about the Julian calendar - it's not even consistent (changed to the calendar system around 45BC for instance).
The datetime
module does take leap years into accout however, if you provide it the year and the day of year:
>>> datetime.strptime("2004 60", "%Y %j")
datetime.datetime(2004, 2, 29, 0, 0)
>>> datetime.strptime("2005 60", "%Y %j")
datetime.datetime(2005, 3, 1, 0, 0)
So you can calculate the date like:
date = datetime.strptime(year+julian, "%Y%j").strftime("%Y-%d-%m")
And all together with the splitting of the values to multiple lines:
from datetime import datetime
# ...
lines = f_open.readlines()
# split string and ignore unwanted elements
for line in lines:
_, year, julian, time, *values = line.split(",")
# format date, convert julian day-of-year to 'day-month'
date = datetime.strptime(year+julian, "%Y%j").strftime("%Y-%d-%m")
time = datetime.strptime(time.rjust(4, "0"), "%H%M").strftime("%H:%M:%S")
timestamp = "%s %s" % (date, time)
with open(targetName, 'a') as target:
for i, value in enumerate(values[:10], 1):
target.write(",".join((timestamp, str(i), value)))
target.write("\n")