How to open and merge multiple csv files on the same input line?
import csv
file=input("Enter the csv files:")
f= open(file, 'r')
r=csv.reader(f)
for row in r:
print(row)
So when entered for 'file' it would be like "Enter the csv files:may.csv,june.csv.july.csv"
So how do you open and merge each of these files? They all have the same header. I know this format is probably really bad, but thanks!
Maybe something like this:
import csv
files = input("Enter the csv files:")
for file in (f.strip() for f in files.split(',')):
with open(file) as f:
r = csv.reader(f)
next(r) # Skips the header
for row in r:
print(row)
I can think of 2 ways to do it. import pandas as pd
a = pd.read_csv("C:\\Users\\rshuell001\\Desktop\\excel_files\\Book1.csv")
b = pd.read_csv("C:\\Users\\rshuell001\\Desktop\\excel_files\\Book2.csv")
b = b.dropna(axis=1)
merged = a.merge(b, on='numbers')
merged.to_csv("C:\\Users\\rshuell001\\Desktop\\excel_files\\Book3.csv", index=False)
OR
import pandas as pd
a = pd.read_csv("C:\\Users\\rshuell001\\Desktop\\excel_files\\Book1.csv")
b = pd.read_csv("C:\\Users\\rshuell001\\Desktop\\excel_files\\Book2.csv")
b = b.dropna(axis=1)
merged = a.merge(b, on='numbers')
merged.to_csv("C:\\Users\\rshuell001\\Desktop\\excel_files\\Book3.csv", index=False)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.