I have a text file just say
text1 text2 text text
text text text text
I am looking to firstly count the number of strings in the file (all deliminated by space) and then output the first two texts. (text 1 text 2)
Any ideas?
Thanks in advance for the help
Edit: This is what I have so far:
>>> f=open('test.txt')
>>> for line in f:
print line
text1 text2 text text text text hello
>>> words=line.split()
>>> words
['\xef\xbb\xbftext1', 'text2', 'text', 'text', 'text', 'text', 'hello']
>>> len(words)
7
if len(words) > 2:
print "there are more than 2 words"
The first problem I have is, my text file is: text1 text2 text text text
But when i pull the output of words I get:
[‘\xef\xbb\xbftext1’, ‘text2’, ‘text’, ‘text’, ‘text’, ‘text’, ‘hello’]
Where does the ‘\xef\xbb\xbf come from?
To read a file line by line, just loop over the open file object in a
forloop:To split a line by whitespace into a list of separate words, use
str.split():To count the number of items in a python list, use
len(yourlist):To select the first two items from a python list, use slicing:
I’ll leave constructing the complete program to you, but you won’t need much more than the above, plus an
ifstatement to see if you already have your two words.The three extra bytes you see at the start of your file are the UTF-8 BOM (Byte Order Mark); it marks your file as UTF-8 encoded, but it is redundant and only really used on Windows.
You can remove it with:
You may want to decode your strings to unicode using that encoding:
You could also open the file using
codecs.open():Note that
codecs.open()will not strip the BOM for you; the easiest way to do that is to use.lstrip():