Sponsored By

Sign in to follow this  
davalend

Retrieve A Data In A File

Recommended Posts

1 5510/2 if then Date = 03/17/1999 03/18/1999

2 2490/1 if DA0=100 then DA1 = 112

3 4728/2 if F2=.AP then F1 = .S .AS

i 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?

Share this post


Link to post
Share on other sites

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 by davalend

Share this post


Link to post
Share on other sites

Well from the look of it, breaking the string on whitespace and taking the second element will do what you want.

Share this post


Link to post
Share on other sites

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/2

or as one line

"1 5510/2 if then Date = 03/17/1999 03/18/1999".split()[1]

Edited by shanenin

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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 loop

read = open('rules.txt', 'r')
a = read.readlines()

arr = []
for ruleline in a:
arr.append(ruleline.split()[1])

print arr

Share this post


Link to post
Share on other sites

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 f

while True:

print g

[/c0de]

in my example I used "0" in place of "o". make sure you just use "o"

Edited by shanenin

Share this post


Link to post
Share on other sites

hmm....

because i am using this number in "a" in a formula.

for example: x = a * 100

so x will return me a list of difference answer with difference input of "a"

get what i mean?

Share this post


Link to post
Share on other sites
hmm....

because i am using this number in "a" in a formula.

for example: x = a * 100

so 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.

Share this post


Link to post
Share on other sites
No, I'm afraid it made no sense whatsoever.

It sounds like he discovered variables.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this