I want to use a file browser to load a few .csv(let's call them csv1) files and run them in my python script. As a result it will copy some columns from csv1 and generate a new .csv file (csv2). I have written the codes for the file browser, and also the script to generate the csv2 file. However, I cannot find a way to use the file browser to select the csv1 and then input it in my script to create the csv2 file.
If anyone knows the solution, please help to comment. Appreciated a lot!!
The following is my codes so far:
from PyQt4 import QtGui
import os, sys
import subprocess
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(600, 300, 400, 200)
self.setWindowTitle('Multiple Browse')
btn = QtGui.QPushButton('Browse', self)
btn.resize(btn.sizeHint())
btn.clicked.connect(self.SingleBrowse)
btn.move(150, 100)
self.show()
def SingleBrowse(self):
filePaths = QtGui.QFileDialog.getOpenFileNames(self,
'Multiple File',
"Desktop",
'*.csv')
def main():
app = QtGui.QApplication(sys.argv)
w = Widget()
app.exec_()
if __name__ == '__main__':
main()
## Below codes are initially from a separate script, but I just combine them
## together in one. Because I think maybe I could call the class function
## "Wideget" somewhere below for using the file browser to select the files?
import pandas as pd
from pandas import DataFrame
df1 = pd.read_csv('csv0.csv')
df2 = pd.read_csv('csv1.csv')
df3 = df1.loc[:, ['a_column', 'b_column']]
df3[""] = ""
df4 = df2.loc[:, ['c_column','d_column' , 'e_column']]
new = pd.concat([df3, df4], axis=1)
new.index = new.index + 1
new.to_csv('csv2.csv')
If you want to get the path of 2 files one by one you have to use getOpenFileName, in addition I understand that they must be different file so we verify that it has not been previously selected
def initUI(self):
# ...
self.csv = []
def SingleBrowse(self):
if len(self.csv) < 2:
filePath = QtGui.QFileDialog.getOpenFileName(self,
'',
"Desktop",
'*.csv')
if filePath != "" and not filePath in self.csv:
self.csv.append(filePath)
print(self.csv)