Godshand
Godshand

Reputation: 631

Any idea how to output tabulate to CSV file?

I just want to figure out how to write the tabulate result into CSV file according to header, because when I write it in my csv, all the result is on one column only, using python 2.7. Is panda framework necessary for this?

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()

Here's a sample output from my console:

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}

Upvotes: 0

Views: 2604

Answers (1)

Raymond Hettinger
Raymond Hettinger

Reputation: 226534

It isn't necessary to use pandas for this.

The standard library already provides a csv module.

The writer in that module has a brief and clear example of how to write out csv.

Adapting it to your example gives:

with open('output.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(content2)

Upvotes: 2

Related Questions