PYTHON脚本集合

less than 1 minute read

Published:

PYTHON SCRIPT COLLECTION

  • SV中GPS时间转换为datatime表示

import csv
import sys
import datetime

'''
convert second + nanosecond to datatime(year mouth day hour minute second) in csv
'''

if __name__ == "__main__":
    input_file_path = sys.argv[1]
    output_file_path = sys.argv[2]
    csv_file_input = csv.reader(open(input_file_path, 'r'))
    csv_file_output = csv.writer(open(output_file_path, 'w'))
    first_row = True
    new_rows = []
    for row in csv_file_input:
        if first_row:
            new_header = ["timestamp"]
            for i in range(2, len(row)):
                new_header.append(row[i])
            csv_file_output.writerow(new_header)
            first_row = False
        else:
            timestamp = int(row[0]) + int(row[1]) / 1e9
            new_timestamp = datetime.datetime.fromtimestamp(timestamp)
            str_new_timestamp = "{}-{:0>2d}-{} {}:{}:{:.9f}".format(
                new_timestamp.year, int(new_timestamp.month), new_timestamp.day,
                new_timestamp.hour, new_timestamp.minute, new_timestamp.second + int(row[1]) / 1e9)
            new_row = [str_new_timestamp]
            for i in range(1, len(new_header)):
                new_row.append(row[i+1])
            csv_file_output.writerow(new_row)