4

Im a recent graduate who has made some contacts and trying to help one with a project. This project will help evolve into something open source for people. So any help I can get would be greatly appreciated.

Im trying to parse out certain items in a file I have succeeded in parsing out the specific items. I now need to figure out how to attach all the remaining data to another variable in an organized way for example.

file=open("file.txt",'r') 
row = file.readlines()
for line in row:
 if line.find("Example") > -1:
 info = line.split()
 var1 = info[0]
 var2 = info[1]
 var3 = info[2]
 remaining_data = ????

^^^^^^^^^^is my sample code already doing 90% of what i need. I want to get the remaining_data to all go into that variable line by line for.

print remaining_data
output:remaining_data{
 line 1 of data
 line 2 of data
 line 3 of data
 line 4 of data
}

how can I get it organized and going in like that line by line?

mechanical_meat
170k25 gold badges238 silver badges231 bronze badges
asked Apr 22, 2013 at 19:48

2 Answers 2

7
remaining_data = []
for line in open("file.txt",'r'):
 if line.find("Example") > -1:
 info = line.split()
 var1 = info[0]
 var2 = info[1]
 var3 = info[2]
 remaining_data.append(' '.join(info[3:]))

at the end of the loop, remaining data will have all lines with out the first 3 elements

answered Apr 22, 2013 at 20:18
Sign up to request clarification or add additional context in comments.

Comments

1

by using a slice

remaining_data=info[3:]

If you need the indices you could do

for i, line in enumerate(info[3:]):
 print("{}: {}".format(i, line))
answered Apr 22, 2013 at 19:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.