'Combine multiple csv files by column without pandas
I need to join 4 csv files into one without using pandas based on specific column. I managed to do it with 2 files. But struggle with adding more files to the joined one. the files have columns:
stoptimes.txt
trip_id,departure_time,stop_id
stops.txt
stop_id,stop_code,stop_name
trips.txt
route_id,trip_id,trip_headsign,vehicle_id
vehicles.txt
vehicle_type_id,vehicle_type_name
desired output is to have all of them in one file I wanted to join stoptimes and stops on stop_id column, then join to this file trip.txt on trip_id column, and then join vehicles file on vehicle_type_id column
stoptimes = []
with open('stop_times.txt', 'r', encoding='utf8') as f:
for line in f:
stoptimes.append(line.split(','))
stops = []
with open('stops.txt', 'r', encoding='utf8') as f:
for line in f:
stops.append(line.split(','))
trips = []
with open('trips.txt', 'r', encoding='utf8') as f:
for line in f:
trips.append(line.split(','))
vehicletypes = []
with open('vehicle_types.txt', encoding='utf8') as f:
vehicletypes.append(line.split(','))
for (trip_id, arrival_time, departure_time, stop_id, stop_sequence, pickup_type, drop_off_type) in stoptimes:
for (stop_id, stop_code, stop_name, stop_lat, stop_lon) in stops:
if stop_id == stop_id:
print(stop_id, stop_name, trip_id, departure_time)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
