[Tutor] text file help

Alan Gauld alan.gauld at btinternet.com
Wed Feb 18 10:14:53 CET 2015


On 18/02/15 07:50, Tihomir Zjajic wrote:
> How  can I get this ; kl_number = 1202, kl_number = 1403, kl_number = 1802, kl_number = 2801, kl_number = 2502, kl_number = 2303, kl_number = 2254, kl_number = 1682, kl_number = 1403
> kl_number = 1304, from text file(formular doznake) . I got this on this way ;

Your mail seems to assume that we will know what you are talking about.

I have no idea what doznake is. I don;t recognize your data sample.

> def vrs_drv():
>      vrs_drv = raw_input("Enter a vrs_drv:")

Its a really bad idea to name a variable the
same as the name of the function.

>      if vrs_drv == "21":
>          return("1")
>      if vrs_drv == "22":
>          return("2")
>      if vrs_drv == "41":
>          return("4")

I have no idea what these numbers are supposed to signify,
they appear to be quite arbitrary and bear no relation
to the numbers in your data above... Also what do you do
if the user enters a different number from 21,31,41?

> number1 = vrs_drv()
> print("kl_number1:",number1)

You show us your code. What does the output look like?

> def prs_prec():
>      prs_prec = raw_input("Enter a prs_prec:")

Again, don't use the same name as the function.

>      if prs_prec == "20":
>          return("20")
>      if prs_prec == "40":
>          return("40")
>      if prs_prec == "80":
>          return("80")

Since you just return the same string as the input
you could have saved a lot of coding.
What you expect to happen if the user enters
something other than 20,40 or 80?

> number2 = prs_prec()
> print("kl_number2:",number2)

Again, can we see the output?

> def teh_kl():
>      teh_kl = raw_input("Enter a teh_kl:")
>      if teh_kl == "1":
>          return("1")
>      if teh_kl == "2":
>          return("2")
>      if teh_kl == ("3"):
>          return("3")
>      if teh_kl == ("4"):
>          return("4")

Exactly the same comments as above.

> number3 = teh_kl()
> print("kl_number3:",number3)
> print(number1+number2+number3)
> print("\n\nPress the enter key to exit.")

OK, Now I see the relationship with the data sample.

> formular doznake.txt
>
> red_broj = 1
> vrs_drv = 21
> prs_prec = 20
> teh_kl = 2
> red_broj = 2
> vrs_drv = 21
> prs_prec = 40
> teh_kl = 3

I assume red_broj indicates the start of a new record?
So you can read three lines from the file after you
find a red_broj entry?

Something like this? (untested)

kl_numbers = []
with open('doznake.txt') as doznake
     for line in doznake:
        if line.startswith('red_broj')
           num = makeNumber(next(doznake),next(doznake),next(doznake))
           kl_numbers.append(num)

def makeNumber(l1,l2,l3):
     nums = []
     for line in (s1,s2,s3):
         nums.append(line.rstrip().split()[-1])
     return ''.join(nums)

That should result in you having a list of the kl_numbers
you asked for in your data sample. If I understood
your question properly.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list