Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

Thread: [PYTHON] Replace a document line

  1. #11
    Join Date
    May 2010
    Beans
    166

    Re: [PYTHON] Replace a document line

    Quote Originally Posted by GTMoraes View Post
    I've took a look at the link.. Looks fairly complicated...
    I'm still trying to make the reserve_seat() to work. I've tried the first script and it's a no-go.
    Alright, let me give you a clue -

    Change the function to this -

    Code:
    def reserve_seat():
        cad = input("Which seat to reserve? ")
        reservation=open("reservation.pycine", "r+", encoding="utf-8")
        for line in reservation:
            print(type(line))
        reservation.close()
    What does that tell you about the "line" variable?
    Is it special in anyway?
    How does it compare to say,
    Code:
    print(type("I am a plain old string"))
    ?

    And then after to this -

    Code:
    def reserve_seat():
        cad = input("Which seat to reserve? ")
        reservation=open("reservation.pycine", "r+", encoding="utf-8")
        seats = reservation.readlines()
        reservation.close()
        for line in seats:
            print(type(line))
    What does that tell you about the file object "reservation"?
    Does it need to be open for this to work, or can it be closed?
    What does this tell you about your function?

    (Note, it's broken in another way too - but I'll get to that after you work this out )

  2. #12
    Join Date
    May 2010
    Beans
    166

    Re: [PYTHON] Replace a document line

    Doh, in the time I wrote the reply, you had edited your text and had found the problem. Well done

    My suggestion to you is to find out how you save any old file - though, it's more a case of writing the file, not saving. See the above debugging process to work that one out

    My clue is that you've already done it once - look into your own code and see what can be reused, baring in mind you will have to write *a new copy* of the file - you can't edit the old one in-place easily.

  3. #13
    Join Date
    Feb 2011
    Location
    Carnaval's land.
    Beans
    Hidden!
    Distro
    Ubuntu 19.04 Disco Dingo

    Re: [PYTHON] Replace a document line

    Quote Originally Posted by TwoEars View Post
    Doh, in the time I wrote the reply, you had edited your text and had found the problem. Well done

    My suggestion to you is to find out how you save any old file - though, it's more a case of writing the file, not saving. See the above debugging process to work that one out

    My clue is that you've already done it once - look into your own code and see what can be reused, baring in mind you will have to write *a new copy* of the file - you can't edit the old one in-place easily.
    Good idea. Overwriting completely the original file, right?
    I'll try doing this. Thanks for the tip =)

  4. #14
    Join Date
    Feb 2011
    Location
    Carnaval's land.
    Beans
    Hidden!
    Distro
    Ubuntu 19.04 Disco Dingo

    Re: [PYTHON] Replace a document line

    lol I'm getting my **** beaten here. If I put the print(line) inside the for loop, it outputs the file just like I want.
    If I put it outside the loop, it'll only print the last seat (16).
    If I make it write inside the loop, the program keeps writing forever.

    This forever write is kinda weird. It outputs the file like this (I've tried to reserve the 10th seat)
    Code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    1
    ...
    9
    R10
    11
    ...
    16
    1
    ...
    9
    RR10
    11
    ...
    16
    1
    ...
    9
    RRR10
    11
    ...
    16
    1
    ...
    9
    RRRR10
    11
    ...
    Hope you get it. The code I used was the following one:
    Code:
    def reserve_seat():
        cad = input("Which seat to reserve? ")
        reservation=open("reservation.pycine", "r+", encoding="utf-8")
        for line in reservation:
            line = line.replace(cad, 'R'+cad)
            reservation.write("%s" % line)
        reservation.close()
    Outside the loop it prints a extra line with "16" (the last original line)

    Looks like I'm too noob for this hehe

    What do you suggest me to do?
    Last edited by GTMoraes; July 19th, 2011 at 06:52 PM.

  5. #15
    Join Date
    May 2010
    Beans
    166

    Re: [PYTHON] Replace a document line

    Quote Originally Posted by GTMoraes View Post
    Good idea. Overwriting completely the original file, right?
    I'll try doing this. Thanks for the tip =)
    Yup, unless you want to spend time looking into seek and other complicated features of file objects.

    Now, for the other tip -

    Say I have the code like you have,

    Code:
    random_numbers = ['1','2','3','10']
    number_reserved = '1'
    for number in random_numbers:
        number = number.replace(number_reserved, 'This number has been edited')
        print(number)
    print(random_numbers)
    How many numbers from random_numbers are edited? Is this what you expected?

    Is random_numbers updated, or is it the same as the original?

    Hope this helps.

  6. #16
    Join Date
    May 2010
    Beans
    166

    Re: [PYTHON] Replace a document line

    Quote Originally Posted by GTMoraes View Post
    lol I'm getting my **** beaten here. If I put the print(line) inside the for loop, it outputs the file just like I want.
    If I put it outside the loop, it'll only print the last seat (16).
    If I make it write inside the loop, the program keeps writing forever.

    This forever write is kinda weird. It outputs the file like this (I've tried to reserve the 10th seat)
    Code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    1
    ...
    9
    R10
    11
    ...
    16
    1
    ...
    9
    RR10
    11
    ...
    16
    1
    ...
    9
    RRR10
    11
    ...
    16
    1
    ...
    9
    RRRR10
    11
    ...
    Hope you get it. The code I used was the following one:
    Code:
    def reserve_seat():
        cad = input("Which seat to reserve? ")
        reservation=open("reservation.pycine", "r+", encoding="utf-8")
        for line in reservation:
            line = line.replace(cad, 'R'+cad)
            reservation.write("%s" % line)
        reservation.close()
    Outside the loop it prints a extra line with "16" (the last original line)

    Looks like I'm too noob for this hehe

    What do you suggest me to do?
    I would suggest first reading the contents, closing the file, reopen the file for writing (completely clearing the file wth 'w'), and then try your code.

  7. #17
    Join Date
    Feb 2011
    Location
    Carnaval's land.
    Beans
    Hidden!
    Distro
    Ubuntu 19.04 Disco Dingo

    Re: [PYTHON] Replace a document line

    Certainly, I'm overlooking something.
    It is replacing correctly, but it's not writing on the file.

    I've tried doing what you suggested:
    Read the contents, close the file, reopen the file for writing (completely clearing the file wth 'w'), and then running it.
    I've did the following:
    Code:
    def reserve_seat():
        cad = input("Which seat to reserve? ")
        reservation=open("reservation.pycine", "r+", encoding="utf-8")
        for line in reservation:
            reservation.close()
            reservation=open("reservation.pycine", "w", encoding="utf-8")
            line = line.replace(cad, 'R'+cad)
            reservation.write("%s" % line)
        reservation.close()
    this returns an I/O Error for me (Operation on closed file).

    Then I tried this:
    Code:
    def reserve_seat():
        cad = input("Which seat to reserve? ")
        reservation=open("reservation.pycine", "r+", encoding="utf-8")
        for line in reservation:
            reservation=open("reservation.pycine", "w", encoding="utf-8")
            line = line.replace(cad, 'R'+cad)
            reservation.write("%s" % line)
        reservation.close()
    It creates a new file... with only the "16" number on the first line.

    I feel like I'm closer to solve this. Thank you for you help so far! I've learned a lot

  8. #18
    Join Date
    May 2010
    Beans
    166

    Re: [PYTHON] Replace a document line

    Code:
    def reserve_seat():
        cad = input("Which seat to reserve? ")
        reservation=open("reservation.pycine", "r+", encoding="utf-8")
        for line in reservation:
            reservation.close()
            reservation=open("reservation.pycine", "w", encoding="utf-8")
            line = line.replace(cad, 'R'+cad)
            reservation.write("%s" % line)
        reservation.close()
    Well, here's another tip - you can read the entire contents of a file using the readlines method, for example -

    Code:
    reservation=open("reservation.pycine", "r+", encoding="utf-8")
    seats = reservation.readlines()
    reservation.close()
    new_file = open("reservation.pycine", "w", encoding="utf-8")
    for seat in seats:
        new_file.write(seat)
    new_file.close()
    Try adapting that for your usage and see my previous comment about the replace method.

  9. #19
    Join Date
    Feb 2011
    Location
    Carnaval's land.
    Beans
    Hidden!
    Distro
    Ubuntu 19.04 Disco Dingo

    Re: [PYTHON] Replace a document line

    Oohhh, so that was it!
    I needed to create another variable to handle the file modification.

    With your help, I've managed to make it work!
    Finally! The program now works!

    I can now proceed with Tkinter. Hope I don't face such little problems hehe

    Again, thank you very much!


    P.S.: Just for the info, I needed to edit the file creation to instead of creating 1,2,3,4..., create 01,02,03..., otherwise when I reserve the seat 5, it would break the 15th seat, by renaming it to 1R5 hehe.
    Code:
    try:
        reservation=open("reservation.pycine", "r+", encoding="utf-8")
        reservation.close()
    except IOError:
        reservation=open("reservation.pycine", "w+", encoding="utf-8")
        for seats in range (1,17):
            reservation.write("%02d\n" % seats)
        reservation.close()

  10. #20
    Join Date
    May 2010
    Beans
    166

    Re: [PYTHON] Replace a document line

    Quote Originally Posted by GTMoraes View Post
    Oohhh, so that was it!
    I needed to create another variable to handle the file modification.

    With your help, I've managed to make it work!
    Finally! The program now works!

    I can now proceed with Tkinter. Hope I don't face such little problems hehe

    Again, thank you very much!


    P.S.: Just for the info, I needed to edit the file creation to instead of creating 1,2,3,4..., create 01,02,03..., otherwise when I reserve the seat 5, it would break the 15th seat, by renaming it to 1R5 hehe.
    Code:
    try:
        reservation=open("reservation.pycine", "r+", encoding="utf-8")
        reservation.close()
    except IOError:
        reservation=open("reservation.pycine", "w+", encoding="utf-8")
        for seats in range (1,17):
            reservation.write("%02d\n" % seats)
        reservation.close()
    That could've been resolved if you changed your
    Code:
            line = line.replace(cad, 'R'+cad)
    to being encapsulated in an if statement.

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •