davalend Posted January 17, 2006 Report Share Posted January 17, 2006 1 5510/2 if then Date = 03/17/1999 03/18/19992 2490/1 if DA0=100 then DA1 = 1123 4728/2 if F2=.AP then F1 = .S .ASi have this data in a txt file. now i need to retrieve 5510/2, 2490/1, 4728/2 ONLY. how do i do that in python? Quote Link to post Share on other sites
davalend Posted January 17, 2006 Author Report Share Posted January 17, 2006 (edited) read = open('sample.txt', 'r')a = read.readlines()arr = []for ruleline in a: for ruledata in ruleline[2:9]: arr.append(ruledata) print arr,what is wrong with this code? it prints ['5'] ['5', '5'] ['5', '5', '1'] ['5', '5', '1', '0'] ['5', '5', '1', '0', '/'] ['5', '5', '1', '0', '/', '2'] ['5', '5', '1', '0', '/', '2', ' '] .....what I want is 5510/2, not seperating them...pls guide me. Edited January 17, 2006 by davalend Quote Link to post Share on other sites
Hai-Etlik Posted January 17, 2006 Report Share Posted January 17, 2006 Well from the look of it, breaking the string on whitespace and taking the second element will do what you want. Quote Link to post Share on other sites
davalend Posted January 17, 2006 Author Report Share Posted January 17, 2006 Sorry, i dont get what u mean ... which part of my coding is wrong? and how shd it be change? Quote Link to post Share on other sites
shanenin Posted January 17, 2006 Report Share Posted January 17, 2006 (edited) as to the method Hai-Etlik mentioned>>> test = "1 5510/2 if then Date = 03/17/1999 03/18/1999">>> test_splitted = test.split()>>> print test_splitted['1', '5510/2', 'if', 'then', 'Date', '=', '03/17/1999', '03/18/1999']>>> print test_splitted[1]5510/2or as one line"1 5510/2 if then Date = 03/17/1999 03/18/1999".split()[1] Edited January 17, 2006 by shanenin Quote Link to post Share on other sites
davalend Posted January 17, 2006 Author Report Share Posted January 17, 2006 tks so much for the quick reply. ya. i done it Quote Link to post Share on other sites
davalend Posted January 17, 2006 Author Report Share Posted January 17, 2006 read = open('rules.txt', 'r')a = read.readlines()arr = []for ruleline in a: arr.append(ruleline.split()[1]) print arr-----------------------------------['5510/2']['5510/2', '2490/1']['5510/2', '2490/1', '4728/2']['5510/2', '2490/1', '4728/2', '1940/1']['5510/2', '2490/1', '4728/2', '1940/1', '1940/1']['5510/2', '2490/1', '4728/2', '1940/1', '1940/1', '5510/3']---------------------------------------------------------------it keeps repeating. how come? Quote Link to post Share on other sites
shanenin Posted January 17, 2006 Report Share Posted January 17, 2006 I am not sure exactly what you are trying to do. I think the problem is you have your print statement in the loop. So everytime it loops it is printing. Just move you print statement past the loopread = open('rules.txt', 'r')a = read.readlines()arr = []for ruleline in a: arr.append(ruleline.split()[1])print arr Quote Link to post Share on other sites
shanenin Posted January 17, 2006 Report Share Posted January 17, 2006 (edited) since python is sensitive to whitespace(indentation), it would help if you placed your code in tags. It would make your code easier to read on this forum, for example:[c0de]for in in test: print fwhile True: print g[/c0de]in my example I used "0" in place of "o". make sure you just use "o" Edited January 17, 2006 by shanenin Quote Link to post Share on other sites
davalend Posted January 17, 2006 Author Report Share Posted January 17, 2006 hmm....because i am using this number in "a" in a formula. for example: x = a * 100so x will return me a list of difference answer with difference input of "a"get what i mean? Quote Link to post Share on other sites
Hai-Etlik Posted January 17, 2006 Report Share Posted January 17, 2006 hmm....because i am using this number in "a" in a formula. for example: x = a * 100so x will return me a list of difference answer with difference input of "a"get what i mean?No, I'm afraid it made no sense whatsoever. Quote Link to post Share on other sites
jcl Posted January 18, 2006 Report Share Posted January 18, 2006 No, I'm afraid it made no sense whatsoever.It sounds like he discovered variables. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.