Python store line by line in List from Text File -


i have text file , process in python

info.txt

    firstname1     surname1     email@email.com1     student1  -------------------     firstname2     surname2     email@email.com2     student2 ----------------- 

i want write python code iterares , stores each line in each indexs example: [firstname,surname,email@email.com,student] , ignore "-----"

python code

 open('log.txt') f:         lines = f.read().splitlines()         x = x + 1      in lines:         print 

but believe wrong amm new python can 1 please point me in correct direction want output me somthing so

output

index 1 :first name: firstname1          surname: surname1          email: email@email.com1          student student1  index 2 :first name: firstname2          surname: surname2          email: email@email.com2          student: student2 

i know it'd better form explain general guidelines of how this, simple task this, code speaks itself, really...

i'd implement this.

from pprint import pprint  # nicer formatting of output.  # sake of self-contained example, # data inlined here. # # `f` replaced `open('log.txt').  f = """     firstname1     surname1     email@email.com1     student1 -------------------     firstname2     surname2     email@email.com2     student2 ----------------- """.splitlines()  data = [] current = none line in f:     line = line.strip()  # remove leading , trailing spaces     if not line:  # ignore empty lines         continue  # skip rest of iteration.     if line.startswith('-----'):  # new record.         current = none  # clear `current` variable         continue  # skip rest of iteration     if current none:  # no current entry?         # can happen either after ----- line, or         # when we're dealing first line of file.          current = []  # create empty list,         data.append(current)  # , push list of data.     current.append(line)  pprint(data) 

the output list of lists:

[['firstname1', 'surname1', 'email@email.com1', 'student1'],  ['firstname2', 'surname2', 'email@email.com2', 'student2']] 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -