[英]Python: How to merge multiple csv files on the same input line?
How to open and merge multiple csv files on the same input line? 如何在同一输入行上打开和合并多个csv文件?
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" 因此,当输入“文件”时,就像“输入csv文件: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
将熊猫作为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 将熊猫作为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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.