Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a csv file in Microsoft Excel in Python?

base_path = os.path.dirname(os.path.abspath(__file__))          
_csvFilename = os.path.join(base_path, "bcForecasting.csv")
_csvFile = open (_csvFilename, 'wb')
_csvFile = csv.writer(_csvFile, quoting=csv.QUOTE_ALL)

_Header = self.makeIntoList (self.root.tss.series () [0].getAllTimes (), self.originalTimesteps + _futurePeriods)
_csvFile.writerow (_Header)

Now I want to open the created bcForecasting.csv file in Excel. How to do it in Python?

like image 978
curiousguy Avatar asked Aug 11 '13 16:08

curiousguy


2 Answers

Usually on Windows the .csv filetype is configured to be opened by Excel. In this case you can just do:

from subprocess import Popen
p = Popen('filename.csv', shell=True)

In case it does not work, try pointing the full path of the Excel application:

subprocess.Popen(r'C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE stack.csv')
like image 200
Saullo G. P. Castro Avatar answered Oct 18 '22 09:10

Saullo G. P. Castro


You can use the 'startfile' command from the os library.

import os
os.startfile('filename.csv')

Do make sure to specify the path of the file or to set the working directory to the path where the file is located.

like image 1
rajat Avatar answered Oct 18 '22 09:10

rajat