writing to excel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick C
    New Member
    • Apr 2007
    • 54

    writing to excel

    Gents (and ladies)...

    I'm trying to write data to an excel file while using python 2.4 on a PC running XP...

    I can do the very basic of putting data in a txt file, when I provide the data. However...

    With help from some of you I've writen a basic Web scrape that goes out and gets a number from a Web site. Is there a way to have my file write the results of the scrape, i feel as if there must be.

    What i'm trying so far, with no avail is:

    >>> writer = csv.writer(open ("some.csv", "wb"))
    >>> writer.writerow s(scrape.findal l(results))

    This is wrong for a ton of reason i'm sure. All it gives me is a file named "Some" with the 1st and only sheet called "some"

    I was hoping that in the 2nd line ...>>> writer.writerow s(scrape.findal l(results))
    would tell it to take the data collected by my scrape and put it in some.csv.

    it of course, does not.

    if i type print writer.writerow s(scrape.findal l(results)) it will return the number I want.

    any thougths, suggestions, riducles?

    pc
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Patrick C
    Gents (and ladies)...

    I'm trying to write data to an excel file while using python 2.4 on a PC running XP...

    I can do the very basic of putting data in a txt file, when I provide the data. However...

    With help from some of you I've writen a basic Web scrape that goes out and gets a number from a Web site. Is there a way to have my file write the results of the scrape, i feel as if there must be.

    What i'm trying so far, with no avail is:

    >>> writer = csv.writer(open ("some.csv", "wb"))
    >>> writer.writerow s(scrape.findal l(results))

    This is wrong for a ton of reason i'm sure. All it gives me is a file named "Some" with the 1st and only sheet called "some"

    I was hoping that in the 2nd line ...>>> writer.writerow s(scrape.findal l(results))
    would tell it to take the data collected by my scrape and put it in some.csv.

    it of course, does not.

    if i type print writer.writerow s(scrape.findal l(results)) it will return the number I want.

    any thougths, suggestions, riducles?

    pc
    The data may be in the buffer and has not been written yet. It's a good practice to explicitly create a file object, pass to the writer instance, and close the file which forces the data to be written.
    Code:
    import csv
    
    dataList = [0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0],\
               [0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 30.0, 30.0, 30.0, 30.0], \
               [0.0, 0.0, 0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 30.0, 30.0],\
               [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 5.0, 10.0, 15.0, 20.0, 25.0]
    
    fn = r'H:\TEMP\temsys\some.csv'
    f = open(fn, 'wb')
    w = csv.writer(f)
    
    w.writerows(dataList)
    f.close()

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      here's another way, but it generates to excel straightaway, making use of external Python module, called pyXLWriter . A simple example on how to use it.
      Code:
      import pyXLWriter as xl
      workbook = xl.Writer("test.xls")
      worksheet = workbook.add_worksheet('Test')
      datalist = [0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0] #eg
      worksheet.write('A1',datalist[0]) #write 0.0 into cell A1
      worksheet.write('B1',datalist[1]) #writer 5.0 into cell B1 and so on
      .....
      worksheet.write('K1',datalist[10])
      workbook.close()
      after running script, test.xls will be produced. This is of course if you don't mind installing modules to your environment

      Comment

      Working...