user192085
user192085

Reputation: 117

Python: Converting a Text File into XLS Spreadsheet

i'm practicing python, and my current challenge is to take a text file that just contains words (like a brute force dictionary file) and make a spreadsheet out of it. I made a csv successfully but not a xls. Below is what I have so far. The problem is it isn't looping over the words which are separated by newlines. I'd attach it if I knew how. It simply puts the number 2 in the spreadsheet about 65000 times. Any help is appreciated.

import xlwt
import os

os.chdir('/sdcard/Scripts')

workbook = xlwt.Workbook('dictionary.xls')

worksheet = workbook.add_sheet('Dictionary')

text_file =  open('words.txt', 'r')
in_file = text_file.read()

i = 0

for line in (in_file):
    while i < 65500:
        worksheet.write(i, 0,     line)
        i +=1

workbook.save('dictionary.xls')

Upvotes: 0

Views: 105

Answers (1)

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

I think the program is looping over the words - it's iterating through every line in in_file. The problem is that within that loop, you are printing to the first 65500 rows with the same line. Then, by the end of that, i = 65500, so when you advance to the next line, the condition i < 65500 is no longer true, so line does not get written.

The code you probably want is just removing the while loop:

for line in (in_file):
    worksheet.write(i, 0,     line)
    i +=1

This way, i will increase by one for every line that is read. No need to put another loop in.

Upvotes: 1

Related Questions