0

In txt file projections.txt like this

0001|AA|17.12.2017.|20:30|21:00|ponedeljak|For a few dolars more|150|true
0002|BB|19.12.2017.|19:30|21:15|sreda|March on the Drina|300|true
0003|GG|20.12.2017.|18:00|19:00|cetvrtak|A fistful of Dolars|500|true
0004|GG|21.12.2017.|21:15|00:00|petak|The Good, the Bad and the Ugly|350|true

How to change string "true" with "false" (delete) in certain line. For example, if I input 0002, how to change in 2nd line. I tried something like this, but it's not working...

def delete_projection():
    with open('projections.txt') as projections:
    #projections = open('users.txt', 'r').readlines()
        delete = input("Input projection code you want to delete: ")
        for i in projections:
            projection = i.strip("\n").split("|")
            if delete == projection[0]:
                projection[8]="false"
                #projections.close()
    with open('projections.txt', 'w+') as projections:
        projections.write("false")
        projections.close()
2
  • you have to read the file as csv, modify & write the full file. There's no such thing as read/write on a text file.* Commented Jan 31, 2018 at 15:20
  • BTW your attempt makes no sense in many aspects. Commented Jan 31, 2018 at 15:21

3 Answers 3

1

your attempt makes no sense. When you're modifying your lines, the modification is lost.

And writing a text file in read/write cannot work. Writing false somewhere in the file cannot work either...

My proposal:

  • use csv module to handle the splitting
  • read the rows as a list of rows
  • store the rows with the modification
  • overwrite the file with the new data

like this:

import csv

def delete_projection():
    with open('projections.txt') as projections:
    #projections = open('users.txt', 'r').readlines()
        delete = input("Input projection code you want to delete: ")
        contents = []
        cr = csv.reader(projections,delimiter="|")
        for row in cr:
            if delete == row[0]:
                row[8]="false"
            contents.append(row)

    with open('projections.txt', 'w',newline="") as projections:
        cw = csv.writer(projections,delimiter="|")
        cw.writerows(contents)
Sign up to request clarification or add additional context in comments.

2 Comments

Keeping whole file content in memory could be troublesome.
It works. Just change 'users.txt' to 'projections.txt'. I edited it to.
0

You can use re:

import re
s = """
0001|AA|17.12.2017.|20:30|21:00|ponedeljak|For a few dolars more|150|true
0002|BB|19.12.2017.|19:30|21:15|sreda|March on the Drina|300|true
0003|GG|20.12.2017.|18:00|19:00|cetvrtak|A fistful of Dolars|500|true
0004|GG|21.12.2017.|21:15|00:00|petak|The Good, the Bad and the Ugly|350|true
""" 
vals = re.findall('(?<=\n)\d+(?=\|)|(?<=^)\d+(?=\|)', s)
statements = re.findall('true|false', s)
new_s = re.sub('true|false', '{}', s)
to_change = '0002'
s = new_s.format(*[('true' if a == 'false' else 'false') if b == to_change else a for a, b in zip(statements, vals)])

Output:

0001|AA|17.12.2017.|20:30|21:00|ponedeljak|For a few dolars more|150|true
0002|BB|19.12.2017.|19:30|21:15|sreda|March on the Drina|300|false
0003|GG|20.12.2017.|18:00|19:00|cetvrtak|A fistful of Dolars|500|true
0004|GG|21.12.2017.|21:15|00:00|petak|The Good, the Bad and the Ugly|350|true

Comments

0

What you are doing here seems about correct to me but you are only modifying the content of the file loaded in memory, not the file on disk.

You need to write each line to a new file as your are processing lines from projection.txt Something like:

with open('projections.txt') as projections:
    with open('projections-new.txt') as projections_new:
        delete = input("Input projection code you want to delete: ")
        for i in projections:
            projection = i.strip("\n").split("|")
            if delete == projection[0]:
               projection[8]="false"
            projections_new.write("|".join(projection) + "\n")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.