R
R
rbs972021-11-24 19:52:20
Python
rbs97, 2021-11-24 19:52:20

How to remove a character at the end of a line?

Good day!

There is a file with the following structure:

__NV__: 1
name: "name"
short_name: "name"
line1: 0
line2: "#updated 20/9/21,\n4845905????,\n4845956????,\n# magaz,\n4872710????,\n#nano,\n4872717????"
line3: "00000*"
line4: 0
line5: ""
line6: 1
line7: 0
line8: ""
line9: 0

The script itself, which parses the file below:

#!/usr/bin/python3

import os

os.system(r' >info_ran')

prefix = '#'

for filename in os.listdir("/home/user/ran_info/ran-09"):
    file1 = open("/home/user/ran_info/ran-09/" + filename, "r")
    lines = file1.readlines()

    for line in lines:
        if "name:" in line and "short_name" not in line:
            with open('info_ran, mode='a+') as f:
                f.write(line + '\n')
        if "line2:" in line:
            for i in line.split("\\n"):
                if "line2" not in i:
                    if not i.startswith(prefix):
                        with open('info_ran', mode='a+') as f:
                            f.write(i + '\n')


After executing the script, the last line is written as 872717????".
How can I remove the character in the last line " ?

Tried variants with f.write(i.strip('"') + '\n'), f.write(i.rstrip('"' + '\n') - doesn't help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-11-24
@rbs97

i.replace('"', '')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question