使用python合并CSV文件

时间:2017-06-15 18:11:12

标签: python csv

我有一个大约200个CSV文件的文件夹。每个只有第一行的值。如何组合文件,以便每个CSV文件成为新数据集中的一行。

例如:

final.csv contains:
[1, 2, 3, 4, 5, 6]
[2, 3, 4, 5, 5, 6]

我想组合CSV,以便最终的CSV看起来像:

import Tkinter
import serial
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
import matplotlib.animation as animation
from collections import deque
import random
import time

class App:
    def __init__(self, master):
        self.t = 0
        self.arduinoData = serial.Serial('com5', 250000, timeout=None)

        frame = Tkinter.Frame(master)

        self.running = False
        self.ani = None

        self.run = Tkinter.LabelFrame(frame, text="Testing", borderwidth=10, relief=Tkinter.GROOVE, padx=10, pady=10)
        self.run.grid(row=0, column=0, padx=20, pady=20)

        self.run_respiration = Tkinter.Button(self.run, text="RUN",bd=10, height=5, width=10, command=self.getData)
        self.run_respiration.grid(row=0, column=0, padx=5, pady=5)

        self.test_options = Tkinter.LabelFrame(frame, text="Test Options", borderwidth=10, relief=Tkinter.GROOVE, padx=10, pady=10 )
        self.test_options.grid(row=0, column=1, padx=20, pady=20)

        self.stop = Tkinter.Button(self.test_options, text="STOP", bd=10, height=5, width=10, command=self.stopTest)
        self.stop.grid(row=0, column=0, padx=5, pady=5)


        self.fig = plt.Figure()
        self.ax1 = self.fig.add_subplot(211)
        self.line0, = self.ax1.plot([], [], lw=2)
        self.line1, = self.ax1.plot([], [], lw=2)
        self.line2, = self.ax1.plot([], [], lw=2)
        self.line3, = self.ax1.plot([], [], lw=2)
        self.ax2 = self.fig.add_subplot(212)
        self.line4, = self.ax2.plot([], [], lw=2)
        self.line5, = self.ax2.plot([], [], lw=2)
        self.line6, = self.ax2.plot([], [], lw=2)
        self.line7, = self.ax2.plot([], [], lw=2)
        self.canvas = FigureCanvasTkAgg(self.fig,master=master)
        self.canvas.show()
        self.canvas.get_tk_widget().grid(row=0, column=4, padx=20, pady=20)
        frame.grid(row=0, column=0, padx=20, pady=20)

    def getData(self):
        if self.ani is None:
            self.k = 0
            self.arduinoData.flushInput()
            self.arduinoData.write("<L>")
            return self.start()
        else:
            self.arduinoData.write("<L>")
            self.arduinoData.flushInput()
            self.ani.event_source.start()
        self.running = not self.running

    def stopTest(self):
        self.arduinoData.write("<H>")
        if self.running:
            self.ani.event_source.stop()
        self.running = not self.running

    def start(self):
        self.xdata = []
        self.pressure1 = []
        self.displacement1 = []
        self.cycle1 = []
        self.pressure2 = []
        self.displacement2 = []
        self.cycle2 = []
        self.pressure3 = []
        self.displacement3 = []
        self.cycle3 = []
        self.pressure4 = []
        self.displacement4 = []
        self.cycle4 = []
        self.k = 0
        self.limit = 300
        self.arduinoData.flushInput()
        self.ani = animation.FuncAnimation(
            self.fig,
            self.update_graph,
            interval=1,
            repeat=True)
        self.arduinoData.write("<L>")
        self.running = True
        self.ani._start()

    def update_graph(self, i):
        if (self.arduinoData.inWaiting()>0):
            self.xdata.append(self.k)
            x = self.arduinoData.readline()
            self.setData(x)
            strip_data = x.strip()
            split_data = x.split("|")
            actuator1 = split_data[0].split(".")
            actuator2 = split_data[1].split(".")
            actuator3 = split_data[2].split(".")
            actuator4 = split_data[3].split(".")
            self.pressure1.append(int(actuator1[0]))
            self.displacement1.append(int(actuator1[1]))
            self.cycle1 = int(actuator1[2])
            self.pressure2.append(int(actuator2[0]))
            self.displacement2.append(int(actuator2[1]))
            self.cycle2 = int(actuator2[2])
            self.pressure3.append(int(actuator3[0]))
            self.displacement3.append(int(actuator3[1]))
            self.cycle3 = int(actuator3[2])
            self.pressure4.append(int(actuator4[0]))
            self.displacement4.append(int(actuator4[1]))
            self.cycle4 = int(actuator4[2])
            self.line0.set_data(self.xdata, self.pressure1)
            self.line1.set_data(self.xdata, self.pressure2)
            self.line2.set_data(self.xdata, self.pressure3)
            self.line3.set_data(self.xdata, self.pressure4)
            self.line4.set_data(self.xdata, self.displacement1)
            self.line5.set_data(self.xdata, self.displacement2)
            self.line6.set_data(self.xdata, self.displacement3)
            self.line7.set_data(self.xdata, self.displacement4)
            if self.k < 49:
                self.ax1.set_ylim(min(self.pressure1)-1, max(self.pressure4) + 1)
                self.ax1.set_xlim(0, self.k+1)
                self.ax2.set_ylim(min(self.displacement1)-1, max(self.displacement4) + 1)
                self.ax2.set_xlim(0, self.k+1)
            elif self.k >= 49:
                self.ax1.set_ylim(min(self.pressure1[self.k-49:self.k])-1, max(self.pressure4[self.k-49:self.k]) + 1)
                self.ax1.set_xlim(self.xdata[self.k-49], self.xdata[self.k-1])
                self.ax2.set_ylim(min(self.displacement1[self.k-49:self.k])-1, max(self.displacement4[self.k-49:self.k]) + 1)
                self.ax2.set_xlim(self.xdata[self.k-49], self.xdata[self.k-1])
            if self.cycle1 >= self.limit:
                self.running = False
                self.ani = None
                self.ani.event_source.stop()
                self.running = not self.running
            self.k += 1



root = Tkinter.Tk()
app = App(root)
root.mainloop()

*每个矩阵都是.csv的一行,每个矩阵都代表一个新的单元格。

谢谢!

2 个答案:

答案 0 :(得分:4)

如果你有bash,你可以使用

cat *.csv > combined.csv

或者,如果你想以python的方式做到这一点:

import csv

with open('combined.csv', 'wb') as out_file:
    writer = csv.writer(out_file)
    for fname in os.listdir('.'):
        with open(fname, 'rb') as in_file:
            for row in csv.reader(in_file):
                writer.writerow(row)

在这里,您需要导航到包含200多个csv文件的目录。

答案 1 :(得分:0)

  • windows cmd - 输入copy * .csv combined.csv

  • linux bash - cat file1.csv file2.csv&gt; combined.csv

  • python - 类似

    fout=open("out.csv","a")
    for num in range(1,201):
        for line in open("sh"+str(num)+".csv"):
             fout.write(line)    
    fout.close()