User avatar
NPDedyukhin
Posts: 21
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Writing to a text file in a specific line.

Wed Oct 09, 2019 4:37 pm

Good afternoon! ;)

I have a variable, it takes a certain value. I know that the value of this variable should always be on the third line in my text file. Always the third line.

It doesn’t matter what is now contained in the third line.

When a variable changes its value, you just need to open the file, delete everything from the third line and write a new value there.

How to do it? :|
Respectfully,
Nikita Dedyukhin

User avatar
B.Goode
Posts: 8503
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Writing to a text file in a specific line.

Wed Oct 09, 2019 4:44 pm

NPDedyukhin wrote:
Wed Oct 09, 2019 4:37 pm
Good afternoon! ;)

I have a variable, it takes a certain value. I know that the value of this variable should always be on the third line in my text file. Always the third line.

It doesn’t matter what is now contained in the third line.

When a variable changes its value, you just need to open the file, delete everything from the third line and write a new value there.

How to do it? :|


Can you clarify your purpose posting these questions please?

Are they genuinely problems that you need help with?

Or are they educational Public Service 'brainteasers' to which you will publish the solution in a few hours time?





[In the specification for the current puzzle you don't mandate anything about the first two lines of the file - do they have to be maintained in an unmodified state?]

Andyroo
Posts: 4465
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Writing to a text file in a specific line.

Wed Oct 09, 2019 5:02 pm

I would also ask how is the line terminated?

CR/LF or CR is more difficult than LF
Binary in the text is even more of a pain
Are you converting numbers to go into the third line?

Does my other leg ring when you pull it?
Need Pi spray - these things are breeding in my house...

gordon77
Posts: 4162
Joined: Sun Aug 05, 2012 3:12 pm

Re: Writing to a text file in a specific line.

Wed Oct 09, 2019 5:24 pm

try this...

Code: Select all

#!/usr/bin/env python3

# open existing file
Lines = []
with open("test.txt","r") as f:
    line = f.readline()
    while line:
        Lines.append(line.strip())
        line = f.readline()

# change the 3rd line

var = 1.234
Lines[2] = str(var)

# write the file again
with open('test.txt', 'w') as f:
   for item in Lines:
       f.write("%s\n" % item)

PhatFil
Posts: 1365
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Writing to a text file in a specific line.

Wed Oct 09, 2019 6:39 pm

the head command with can deliver the top 2 lines from the file into a new replacement,
you can then echo Value >> into the 3rd line, and as long as you know how many lines are in the file you can tail the remainder into the replacement file..
if 10 lines in your file??

Code: Select all

head -2 > yournewfile.txt
echo "newline3" >> yournewfile.txt
tail -7 >> yournewfile.txt
https://www.tecmint.com/view-contents-of-file-in-linux/

use the wc 'wordcount' command to extract the number of lines in a file tho i cant recall the switch/flag s? required but there is some homework for you ;), but it shouldnt add more than a few lines..

User avatar
NPDedyukhin
Posts: 21
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Re: Writing to a text file in a specific line.

Wed Oct 09, 2019 7:13 pm

gordon77 wrote:
Wed Oct 09, 2019 5:24 pm
try this...

Code: Select all

#!/usr/bin/env python3

# open existing file
Lines = []
with open("test.txt","r") as f:
    line = f.readline()
    while line:
        Lines.append(line.strip())
        line = f.readline()

# change the 3rd line

var = 1.234
Lines[2] = str(var)

# write the file again
with open('test.txt', 'w') as f:
   for item in Lines:
       f.write("%s\n" % item)

Yes, it did. I did about the same.

Here is part of my code:

Code: Select all

file_1 = open("/home/pi/Desktop/pi_cam/settings.txt", "r")
lines = file_1.readlines()
lines[2] = str(a) + '\n'
file_1.close()

file_2 = open("/home/pi/Desktop/pi_cam/settings.txt", "w")
file_2.writelines(lines)
file_2.close()
Respectfully,
Nikita Dedyukhin

User avatar
NPDedyukhin
Posts: 21
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Re: Writing to a text file in a specific line.

Wed Oct 09, 2019 7:16 pm

B.Goode wrote:
Wed Oct 09, 2019 4:44 pm
NPDedyukhin wrote:
Wed Oct 09, 2019 4:37 pm
Good afternoon! ;)

I have a variable, it takes a certain value. I know that the value of this variable should always be on the third line in my text file. Always the third line.

It doesn’t matter what is now contained in the third line.

When a variable changes its value, you just need to open the file, delete everything from the third line and write a new value there.

How to do it? :|


Can you clarify your purpose posting these questions please?

Are they genuinely problems that you need help with?

Or are they educational Public Service 'brainteasers' to which you will publish the solution in a few hours time?





[In the specification for the current puzzle you don't mandate anything about the first two lines of the file - do they have to be maintained in an unmodified state?]

The goal is to place data “on the shelves”.

Certain information must correspond to each line: for example, in the first line we will store information about the type of connection, in the second line information about the network name and so on.

The data request can be repeated, and in order to avoid duplication of data, they should be stored in a strictly defined place for them.
Respectfully,
Nikita Dedyukhin

Andyroo
Posts: 4465
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Writing to a text file in a specific line.

Wed Oct 09, 2019 8:13 pm

Look at SQL (MariaDB or SQLite) rather than flat files.

If you really want to use flat files then look at ini file processing with the configparser module or json processing

Heck, you could even look at pickle and store the data in a Python dictionary.

I think you may do best to learn a little more about the basics of the language via YouTube or a formal course rather than firing off random limited scope questions as your resultant code could well be an unsupportable mess.
Need Pi spray - these things are breeding in my house...

blimpyway
Posts: 168
Joined: Mon Mar 19, 2018 1:18 pm

Re: Writing to a text file in a specific line.

Thu Oct 10, 2019 7:30 am

Only if these files and their formatting are already generated/used by applications out of your control makes sense to use a cryptic, specialised flat file format for structured data.

Otherwise I would take Andyroo's advice, use a standard one like json.

gordon77
Posts: 4162
Joined: Sun Aug 05, 2012 3:12 pm

Re: Writing to a text file in a specific line.

Thu Oct 10, 2019 7:45 am

NPDedyukhin wrote:
Wed Oct 09, 2019 7:13 pm



Yes, it did. I did about the same.

Here is part of my code:

Code: Select all

file_1 = open("/home/pi/Desktop/pi_cam/settings.txt", "r")
lines = file_1.readlines()
lines[2] = str(a) + '\n'
file_1.close()

file_2 = open("/home/pi/Desktop/pi_cam/settings.txt", "w")
file_2.writelines(lines)
file_2.close()
Good you got it working for what you require. As others have said there are many other ways you could use but if it meets your requirements at the moment that's fine.

jerrm
Posts: 194
Joined: Wed May 02, 2018 7:35 pm

Re: Writing to a text file in a specific line.

Thu Oct 10, 2019 1:53 pm

sed -i '3s/.*/newvalue/' file

Return to “Python”