2021年2月4日星期四

Python - add a timestamp column if meet condition in csv file

I have a csv file that look like this:

       MAC  bc:95:50:0a:82:80  bc:95:50:0a:82:80  bc:95:50:0a:82:80  bc:95:50:0a:82:80    bc:95:50:0a:85:60  bc:95:50:0a:85:60  bc:95:50:0a:85:60  bc:95:50:0a:85:60  bc:95:50:0a:85:60    bc:95:50:9e:58:40  bc:95:50:9e:58:40  bc:95:50:9e:58:40  bc:95:50:9e:58:40  bc:95:50:9e:58:40  

There are 3 MAC addresses in my csv file and I want to add a 5 mins timestamp for each MAC address like this:

        MAC             Time  bc:95:50:0a:82:80   2020-11-30 7:05  bc:95:50:0a:82:80   2020-11-30 7:10  bc:95:50:0a:82:80   2020-11-30 7:15  bc:95:50:0a:82:80   2020-11-30 7:20    bc:95:50:0a:85:60   2020-11-30 7:05  bc:95:50:0a:85:60   2020-11-30 7:10  bc:95:50:0a:85:60   2020-11-30 7:15  bc:95:50:0a:85:60   2020-11-30 7:20  bc:95:50:0a:85:60   2020-11-30 7:25    bc:95:50:9e:58:40   2020-11-30 7:05  bc:95:50:9e:58:40   2020-11-30 7:10  bc:95:50:9e:58:40   2020-11-30 7:15  bc:95:50:9e:58:40   2020-11-30 7:20  

Right now I can only generate the timestamp:

dt = datetime.datetime(2020, 11, 30, 7, 5, 0)  end = datetime.datetime(2020, 11, 30, 23, 59, 59)  step = datetime.timedelta(minutes=5)    result = []  while dt < end:      result.append(dt.strftime('%Y-%m-%d %H:%M:%S'))      dt += step  wtr = csv.writer(open ('out.csv', 'w'), delimiter=',', lineterminator='\n')  for x in result:      wtr.writerow ([x])  

But I don't know how to add these timestamps to the MAC column.

https://stackoverflow.com/questions/66056440/python-add-a-timestamp-column-if-meet-condition-in-csv-file February 05, 2021 at 09:42AM

没有评论:

发表评论