i’ve a file like the following:
01/31/2021 08:51:41.222 5777 40.26 01/31/2021 08:51:41.441 5802 40.26 01/31/2021 08:51:41.644 5786 40.26 01/31/2021 08:51:41.847 5793 40.29 01/31/2021 08:51:42.050 5776 40.29 01/31/2021 08:51:42.269 5798 40.29 01/31/2021 08:51:42.456 5791 40.26 01/31/2021 08:51:42.660 5800 40.26 01/31/2021 08:51:42.878 5781 40.26 01/31/2021 08:51:43.081 5793 40.29 01/31/2021 08:51:43.300 5795 40.29 01/31/2021 08:51:43.519 5799 40.29
Named for example file.txt, i’m just interested in date and time and the third value; not interested with the last one. Like for example 01/31/2021 08:51:42.878 5781. So i’ve a time and a value i’m interested with. My aim is to select a range of time and save all the things in this range into a new file because i’ve a really long list in this first file, like datas from the whole day but i’m interested in something like 50 minutes. Imagine from the given example i want only to save data at 8:51:42 and delete the other things. I want to have a new file with this lines in the end (to be honest the last value, the temperature, is not needed):
01/31/2021 08:51:42.269 5798 40.29 01/31/2021 08:51:42.456 5791 40.26 01/31/2021 08:51:42.660 5800 40.26 01/31/2021 08:51:42.878 5781 40.26
That’s what i’m trying to code:
start = (input('Insert starting time for your range:')) end = (input('Insert ending time for your range:')) with open(r"C:UsersUsernameDesktopfile.txt", "r") as fp: lines = fp.readlines() date = [line.split()[0] for line in lines] hours = [line.split()[1] for line in lines] signal = [float(line.split()[2]) for line in lines] temperature = [float(line.split()[3]) for line in lines] full_time = [d + h for d,h in zip(date, hours, start, end)] datetime_object = [datetime.strptime(ft, '%d/%m/%Y%H:%M:%S.%f') for ft in full_time] bad_time = ['start','end'] with open('file.txt') as oldfile, open('newfile.txt', 'w') as newfile: for line in oldfile: if not any( ): newfile.write(line)
I’m experiencing problem already when manually writing start and end date and time because there are problems while converting into the datetime_object. But i’m also wondering how can i remove all the lines i’m not interested with and finally save my file. Infact there are missing stuff on this topic in the code, i was trying to find a solution but already got headache so I’ve decided to ask for help here instead of exploring pages with the search bar :p All the help or external documentations would be appreciated. I’m pretty a newbie with python, don’t eat me please if possible :)
edit: What i want to do is: the user define start time and end time, then save lines from the files between start and end, delete the 4th value and write all into a new file.
Advertisement
Answer
start = (input('Insert starting time for your range:')) end = (input('Insert ending time for your range:')) startDate, startTime = start.split(" ") startMonth, startDay, startYear = startDate.split("/") endDate, endTime = end.split(" ") endMonth, endDay, endYear = endDate.split("/") with open("a.txt", "r") as fp: lines = fp.readlines() dates = [line.split()[0] for line in lines] times = [line.split()[1] for line in lines] signals = [float(line.split()[2]) for line in lines] final = [] for date, time, signal in zip(dates, times, signals): month, day, year = date.split("/") if startYear <= year <= endYear: if startMonth <= month <= endMonth: if startDay <= day <= endDay: if startTime <= time <= endTime: final.append(f"{date} {time} {signal}") print(final) with open("z.txt", "w") as f: for line in final: print(line) f.write(line + "n")