Python 知道如何将表格输出到CSV文件吗?
我只想弄清楚如何根据标题将表格结果写入CSV文件,因为当我在CSV中写入表格结果时,使用python 2.7,所有结果仅在一列中。panda框架对此有必要吗Python 知道如何将表格输出到CSV文件吗?,python,csv,Python,Csv,我只想弄清楚如何根据标题将表格结果写入CSV文件,因为当我在CSV中写入表格结果时,使用python 2.7,所有结果仅在一列中。panda框架对此有必要吗 from tabulate import tabulate class Finder: def print_extensions(self): c = Chrome(self.os) print tabulate(c.extensions(), headers="keys") if sel
from tabulate import tabulate
class Finder:
def print_extensions(self):
c = Chrome(self.os)
print tabulate(c.extensions(), headers="keys")
if self.os == WinBrowsers:
i = InternetExplorer(self.os)
content2 = tabulate(i.extensions(), headers="keys")
text_file=open("output.csv","w")
text_file.write(content2)
text_file.close()
以下是我的控制台的示例输出:
version name id
------------ ----------------------------- --------------------------------
0.10 Slides aapocclcgogkmnckokdopfmhonfmgoek
0.10 Docs aohghmighlieiainnegkcijnfilokake
path name id
------------------------------------------------------------------------------- --------------------------------------------- --------------------------------------
C:\Windows\SysWOW64\ieframe.dll Microsoft Url Search Hook {CFBFAE00-17A6-11D0-99CB-00C04FD64497}
C:\Program Files\Trend Micro\TMIDS\bhoDirectPass32.dll Password Manager ToolBar {97EE74D2-C351-4ECE-B75A-8CD36FAE3661}
没有必要用熊猫来做这个
标准库已经提供了一个模块
该模块中的有一个简单明了的示例,说明如何写出csv
根据您的示例对其进行调整,可以得到:
with open('output.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(content2)