I'm trying to process a list contained within a separate text file. I am able to read the file, and generate an output containing the contents of the file, but the output generated is the unprocessed version, identical to the input. I do not encounter this problem when the list is contained within the module that I'm working with - the process runs successfully. Any ideas why using the input file is causing problems?

This runs successfully (please note - for both runs, "correct" is a function within the same module):

import sys
sys.stdout = open('out.txt','w')

def spelltest():
for i in test:
print correct(i)

test = ['acess','accesing','accomodation','acommodation','acomodation',
'arrangeing','aranging','arragment','articals',
'annt','anut','arnt','auxillary','avaible',
'awfall','afful','basicaly','begining',
'benifit','benifits','beetween',
'bicycal','bycicle','bycycle']

if __name__ == '__main__':
print spelltest()


This does not:

import sys
sys.stdout = open(r'C:\Python26\out.txt','w')

infile = open('C:\\Python26\\text.txt','r')

def spelltest():
for line in infile:
print correct(line)

if __name__ == '__main__':
print spelltest()


The text file contains a list of the following: ['acess', 'accesing', 'accomodation', 'acommodation', 'acomodation', 'acount', 'adress', 'adres', 'addresable', 'aranged', 'arrainged']

Any thoughts on what the issue may be?

Edited by foodstamps: Fixed title

3
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by foodstamps

Any ideas why using the input file is causing problems?

No, because we have no idea what the problem is or what the output to the file actually is compared to what it should be. Add some print statements in "correct()" to see what it is doing differently. Also, note that files opened with

infile = open('C:\\Python26\\text.txt','r')


do not contain lists but a string or strings.

You need to
1 - remove the linefeeds from the input line (line=line.strip('\n'))
2 - split the line into a list (lstLine=line.split(','))

I got it figured out. The input file was the problem, as the contents were in list format. Thank you both for your advice.