2

I would like to write each value in a Python list to a new row in an Excel cell. The object I am generating the list from does not support indexing which is giving me an issue. If my list only generates one value, then there is no problem, however when I generate multiple values, I get multiple errors. I am a Python beginner using 64 bit OS X and Python 2.7. Any help would be much appreciated.

    import mlbgame
    month = mlbgame.games(2016, 7, 21, home ="Pirates")
    games = mlbgame.combine_games(month)
    for game in games:
        print(game)

    import xlwt
    from datetime import datetime

    wb = xlwt.Workbook()
    ws = wb.add_sheet('PiratesG')

    for game in games:
        ws.write(0, 0, str(game))
       #ws.write(1, 0, str(game[1]))

    wb.save('Pirates.xls')

    Error: Traceback (most recent call last):
      File "/Users/Max/Code/PiratesGameToExcel.py", line 14, in <module>
        ws.write(0, 0, str(game))
      File "/usr/local/lib/python2.7/site-packages/xlwt/Worksheet.py", line 1088, in write
        self.row(r).write(c, label, style)
      File "/usr/local/lib/python2.7/site-packages/xlwt/Row.py", line 241, in write
        StrCell(self.__idx, col, style_index, self.__parent_wb.add_str(label))
      File "/usr/local/lib/python2.7/site-packages/xlwt/Row.py", line 160, in insert_cell
        raise Exception(msg)
    Exception: Attempt to overwrite cell: sheetname=u'PiratesG' rowx=0 colx=0    

1 Answer 1

2

You can use enumerate() to get the index of the list. Then you can write to successive rows like so:

for i,game in enumerate(games):
    ws.write(i,0,str(game))
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers, mate. Happy coding to you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.