CurtLH
CurtLH

Reputation: 2417

Write column headers with xlwtin Python

I am trying to parse some Twitter Streaming API data and write specific fields to a column within an Excel workbook. Using xlwt, I've been able to write the columns to the Excel document, but I haven't found a good way to include the column headers.

I know what want each column to be labels in row 1, so it is not necessary to parse column names from the original data. Column 1 header should be "user_screen.name" and column 2 header should be "user_lang"

import sys
import json
import xlwt


data = []


for line in open(sys.argv[1]):
    try:
        data.append(json.loads(line))
    except:
        pass

authors = [i['user']['screen_name'] for i in data]
lang = [i['lang'] for i in data]

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet 1')

column_number = 0
for row_number, item in enumerate(authors):
    worksheet.write(row_number, column_number, item)

column_number = 1
for row_number, item in enumerate(lang):
    worksheet.write(row_number, column_number, item)

workbook.save('tweets.xls')

Upvotes: 0

Views: 5536

Answers (1)

ChrisProsser
ChrisProsser

Reputation: 13098

If you just want static hardcoded headers you can do this quite easily with minor adjustments to you program (see below):

import sys
import json
import xlwt

data = []

for line in open(sys.argv[1]):
    try:
        data.append(json.loads(line))
    except:
        pass

authors = [i['user']['screen_name'] for i in data]
lang = [i['lang'] for i in data]

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet 1')

column_number = 0
worksheet.write(0, column_number, 'Authors')
for row_number, item in enumerate(authors):
    worksheet.write(row_number+1, column_number, item)

column_number = 1
worksheet.write(0, column_number, 'Language')
for row_number, item in enumerate(lang):
    worksheet.write(row_number+1, column_number, item)

workbook.save('tweets.xls')

Note: I have not been able to fully test without the input file, but I think this should work.

Upvotes: 1

Related Questions