简体   繁体   English

如何在 Python 中的 Microsoft Excel 中打开 csv 文件?

[英]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.现在我想在 Excel 中打开创建的bcForecasting.csv文件。 How to do it in Python?如何在 Python 中做到这一点?

Usually on Windows the .csv filetype is configured to be opened by Excel.通常在 Windows 上, .csv文件类型被配置为由 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:如果它不起作用,请尝试指向 Excel 应用程序的完整路径:

subprocess.Popen(r'C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE stack.csv')

You can use the 'startfile' command from the os library.您可以使用 os 库中的“startfile”命令。

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.请务必指定文件的路径或将工作目录设置为文件所在的路径。

On a mac, use the following:在 Mac 上,使用以下命令:

import os
os.system('open filename.csv')

Make sure to specify the path of the file or to set the working directory to the path where the file is located.确保指定文件的路径或将工作目录设置为文件所在的路径。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM