简体   繁体   English

Python-从多个.csv文件中获取列并将其粘贴到新的.csv主文件中

[英]Python- Taking columns from multiple .csv files and paste them into new .csv master file

SO here is the thing, I have 4 .csv files to which I want to copy one column from each of these 4 files and merge them into one single column as the first column of a new .csv master file. 所以这就是问题,我有4个.csv文件,我要从这4个文件中的每一个复制一个列,并将它们合并为一个新的.csv主文件的第一列。 Then, repeat the same process again with another set of different columns and store it as a second column of the master file, and do it again at least 12 more times. 然后,使用另一组不同的列再次重复相同的过程,并将其存储为主文件的第二列,然后再执行至少12次。

Here is my code so far: 到目前为止,这是我的代码:

CODE- 码-

import os
import csv
import datetime as dt
from os import listdir
from os.path import join 
import matplotlib.pyplot as plt



#get the list of files in mypath and store in a list

mypath = 'C:/Users/Alan Cedeno/Desktop/Test_Folder/'
onlycsv = [f for f in listdir(mypath) if '.csv' in f]

#print out all the files with it's corresponding index

for i in range(len(onlycsv)):
print(i,onlycsv[i])

#prompt the user to select the files

option = input('please select file1 by number: ')
option2 = input('please select file2 by number: ')

#build out the full paths of the files and open them

fullpath1 = join(mypath, onlycsv[option])
fullpath2 = join(mypath, onlycsv[option2])

#create third new.csv file

root, ext = os.path.splitext(fullpath2)
output = root + '-new.csv'

with open(fullpath1) as r1, open(fullpath2) as r2, open(output, 'a') as w:
    writer = csv.writer(w)
    merge_from = csv.reader(r1)
    merge_to = csv.reader(r2)
    # skip 3 lines of headers
    for _ in range(3):
    next(merge_from)
    for _ in range(1):
        next(merge_to)
    for merge_from_row, merge_to_row in zip(merge_from, merge_to):
        # insert from col 0 as to col 0
        merge_to_row.insert(1, merge_from_row[2])
        # replace from col 1 with to col 3
        #merge_to_row[0] = merge_from_row[2]
        # delete merge_to rows 5,6,7 completely
        #del merge_to_row[5:8]
        writer.writerow(merge_to_row)

Any suggestions? 有什么建议么?

PLease let me know if the post needs formatting. 请让我知道帖子是否需要格式化。

what are you trying to do here? 您想在这里做什么? if you want to merge all csv to one as it is then it will be good. 如果您想将所有csv合并为原样,那就很好了。

with open(fullpath1) as r1, open(fullpath2) as r2, open(output, 'a') as fout:
   for line in r1:
      fout.write(line)
   for line in r2:
      fout.write(line)
   fout.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 提取根据多个文本文件中的数据计算得出的输出,并将其写入CSV文件的不同列 - Taking output calculated from data in multiple text files and writing them into different columns of a CSV file python从两个csv文件中提取列并将其合并为一个新的csv文件 - python take columns from two csv files and combine them for a new csv file 有没有更简单的方法从多个 CSV 文件中过滤出行并将它们粘贴到新的 csv 文件中? 我在使用 for 循环时遇到问题 - Is there an easier way to filter out rows from multiple CSV files and paste them into a new csv file? IM having issues doing that using a for loop Python-将多个文件导入单个.csv文件 - Python- Import Multiple Files to a single .csv file Python-遍历csv文件中的列 - Python- Iterating through columns in csv file python-将多个标头写入csv文件 - python- writing multiple header to csv file 从多个 CSV 文件计算列并将结果保存到新文件中 - Calculate columns from multiple CSV files and save results into a new file 从多个 CSV 文件创建 5 个新列 - Create 5 new columns from multiple CSV files 将多个文本文件中的列与 csv 列文件 python 进行比较 - compare columns from multiple text files, to csv column file python Python 代码:从多个 csv 文件中提取单个列以另存为新的 csv 文件,而 column_header == source_csv_files - Python code: Extract single columns from multiple csv files to save as a new csv file while column_header == source_csv_files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM