Skip to content Skip to sidebar Skip to footer

Concatenate/join Rows In Txt File W/ Python 3

Looking to clean up a .txt file from NMEA GPS. My current code is below. deletes = ['$GPGGA', '$GPGSA', '$GPGSV', '$PSRF156', ] searchquery = '$GPRMC' with open('Drive_home.txt_rf

Solution 1:

You can do something like:

deletes = ['$GPGGA', '$GPGSA', '$GPGSV', '$PSRF156', ]
searchquery = '$GPRMC'withopen('Drive_home.txt_rf') as f1, open('Drive_home_1.txt', 'w+') as f2:
    lines = f1.readlines()
    for i, line inenumerate(lines):
        if line.startswith(searchquery) andnotany(delete in lines[i + 1] for delete in deletes):
            f2.write(f'{line.rstrip()}{lines[i+1]}')

think it will work, no?

Post a Comment for "Concatenate/join Rows In Txt File W/ Python 3"