This is an easy example about getting the xxx values in column1 and yyy values in column2.
Important! Your file data has to be something like:
xxx yyy xxx yyy xxx yyy
4 spaces between group(xxx yyy xxx yyy) and 1 between each pair data(xxx yyy)
You can use for example another separator logic like this:
xxx,yyy/xxx,yyy/xxx,yyy
And you only have to change data_separator=','
and column_separator='/'
or
xxx-yyy/xxx-yyy/xxx-yyy
And you only have to change data_separator='-'
and column_separator='/'
def read(file):
column1=[]
column2= []
readfile = open(file, 'r')
data_separator = ' ' # one space to separate xxx and yyy
column_separator = ' ' # 4 spaces to separate groups xxx,yyy xxx,yyy
for line in readfile.readlines(): # In case you have more than 1 line
line = line.rstrip('\n') # Remove EOF from line
print line
columns = line.split(column_separator) # Get the data groups
# columns now is an array like ['xxx yyy', 'xxx yyy', 'xxx yyy']
for column in columns:
if not column: continue # If column is empty, ignore it
column1.append(column.split(data_separator)[0])
column2.append(column.split(data_separator)[1])
readfile.close()
I have a text file containing xxx yyy aaa bbb ttt hhh
after calling the function the result is:
column1 = ['xxx', 'aaa', 'ttt']
column2 = ['yyy', 'bbb', 'hhh']