1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Manipulating specific lines in a text file depending on the data in that line

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to manipulate a text file to convert model file syntax between two pieces of software. There are several elements to append in the text file. For instance, I could have a line as follows:

    REALWORKING\K1088 0.995

    This would need to be converted to:

    'R088' 0.995

    All instances of this type of data are preceded with R, take the last 3 digits of the K value and wrap inside apostrophes while also keeping whatever the final number on the line is at the end. In these lines, REALWORKING\K1 is constant, but it could be any 3 digits following, and could be any number of any length of digits at the end of the line.

    I could also have something such as:

    BRICKS\BRICK10\SET01\INPUT\K100103 15512.0

    This would need to be converted to:

    'B10 01 03' 15512.0

    All instances of this type of data are preceded with B, split the digits in the K value (into 3 pairs) and again, wrap that inside apostrophes while keeping whatever the final number on the line is.

    It should also be noted that some of these lines are commented out but need to be converted while remaining commented (ie are preceded with a '%' or '% ' and need to keep the % in place, but convert the data to the new syntax).

    So as an example, I could have something like:

    REALWORKING\K1088 0.995
    % REALWORKING\K1047 123.98754
    BRICKS\BRICK10\SET01\INPUT\K100103 15512.0


    And the required output would be as follows:

    'R088' 0.995
    % 'R047' 123.98754
    'B10 01 03' 15512.0


    I can easily replace the basics using something such as this:

    with open(r"File.txt",'r') as file:
    file_data = file.read()
    file_data = file_data.replace("REALWORKING\K1","'R")
    with open(r"File.txt",'w') as file:
    file.write(file_data)


    However, I cannot complete the manipulation for that line as I cannot search for specific numbers to add the final apostrophe in.

    I have also been going round in circles using .readlines() and for loops which can pick up what needs changing, but I cannot work out how to actually manipulate the data and generate the output required.

    Using something such as this returns a 1 in the correct locations showing it has found the required lines to edit, but I have not managed to figure out how to make the edits on top of this.

    with open(r"File.txt",'r+') as file:
    file_data = file.readlines()
    line_no = 0
    for line in file_data:
    line_no += 1
    if "REALWORKING\K1" in line:
    print(1)
    line = line.replace("REALWORKING\K1","'R") # this does not work, but kept in for example
    line = line.replace(" ", "' ") # likewise here
    else:
    print(0)


    Any help on this would be greatly appreciated!

    Continue reading...

Compartilhe esta Página