I have a CSV file with several entries, and each entry has 2 unix timestamp formatted dates.
I have a method called convert(), which takes in the timestamp and converts it to YYYYMMDD.
Now, since I have 2 timestamps in each line, how would I replace each one with the new value?
EDIT: Just to clarify, I would like to convert each occurrence of the timestamp into the YYYYMMDD format. This is what is bugging me, as re.findall() returns a list.
I assume that by “unix timestamp formatted date” you mean a number of seconds since the epoch. This assumes that every number in the file is a UNIX timestamp. If that isn’t the case you’ll need to adjust the regex:
This reads from stdin and calls convert on each number found.
The “trick” here is that
re.subcan take a function that transforms from a match object into a string. I’m assuming your convert function expects an int and returns a string, so I’ve used a lambda as an adapter function to grab the first group of the match, convert it to an int, and then pass that resulting int to convert.