I have this:
fin = open(blah)
fin_lower= fin.readlines()
lines = [fin_lower.lower() for line in fin]
lines = line.split()
It gives:
TypeError: expected string or buffer
Is it wrong to readlines?
readlines returns a list containing all lines of data, it looks like you have a bug, and you probably want to do this:
lines = [line.lower() for line in fin_lower]
Your code is also mixing variables around, take a good step through it, what are you trying to accomplish? You seem to mix line and lines a bunch.
re.sub expects a string as the third argument, you gave it lines which is a list. Also, you're iterating over fin after consuming all lines of it with readlines. You seem to be trying to do:
with open(blah) as fin:
lines = [line.lower().replace(',', '').split() for line in fin]
Also note that you don't need re to do a literal replacement.
lower(). Forgot it at first.lines is now a list of lists because of split(). You can try printing things to understand what's going on in your script.remove method, lists do. How can I help?split and startswith work with strings and how remove and for loops work with lists.I agree with Bartek
I was able to get this done.
import os
import signal
import time
import sys
import re
import string
fin = open('blah','r')
fin_lower= fin.readlines()
lines=""
for line in fin_lower:
lines += line.lower()
line = re.sub(';',' ',lines)
lines = line.split()
print lines
Initial Contents of File blah
VISHAL; KHIALANI; NONOE; CANGETITDONE;
Final Output
['vishal', 'khialani', 'nonoe', 'cangetitdone']