Monday, March 30, 2015

extract every field from a text file via python

'''
extract every word from a text file separated by space


input.txt file

aa bb cc dd ee ff gg
11 22 33 44 55 66 77
I like to fish

Result output

aa
bb
cc
dd
ee
ff
gg
11
22
33
44
55
66
77
I
like
to
fish
,,,


file = open('input.txt','r')

lines = file.readlines()

for line in lines:
    words = line.split(" ")
    print words
    i = 0
    ilen = len(words)
    while (i < ilen):
          print words[i].rstrip()
          i = i + 1

No comments:

Post a Comment