How to change the value of a line in specific column in a txt file Python?












1















I have a text file with 1 column of different values (floating and integer) and I would like to change a value according to its line. my code is as follows: but I did it knowing what value I should replace, how to do it according to the line?



with open('table1.txt', 'r+') as file:
text = file.read()
i = text.index('3.6') # 3.6 = old value
file.seek(0)
file.write(text[:i] + '7.84' + text[i + len('3.6'):]) # 7.84 = new value


Secondly, how can I choose a row when there are several columns in my file? For example, column 2 row 1? ? thanx










share|improve this question

























  • This is not a python question. Please move this question to unix.stackexchange.com or similar sites

    – Kevin Fang
    Nov 23 '18 at 3:06











  • so you are asking how to use sed or how to process your txt file with python?

    – Enix
    Nov 23 '18 at 3:06











  • Yes, I'm sorry, I was wrong, I just corrected now is my good question

    – F.Moab
    Nov 23 '18 at 3:27













  • you can use file.readline() or use iterator on file object to read line by line and use var count to count line no

    – Ravindhar Konka
    Nov 23 '18 at 4:52
















1















I have a text file with 1 column of different values (floating and integer) and I would like to change a value according to its line. my code is as follows: but I did it knowing what value I should replace, how to do it according to the line?



with open('table1.txt', 'r+') as file:
text = file.read()
i = text.index('3.6') # 3.6 = old value
file.seek(0)
file.write(text[:i] + '7.84' + text[i + len('3.6'):]) # 7.84 = new value


Secondly, how can I choose a row when there are several columns in my file? For example, column 2 row 1? ? thanx










share|improve this question

























  • This is not a python question. Please move this question to unix.stackexchange.com or similar sites

    – Kevin Fang
    Nov 23 '18 at 3:06











  • so you are asking how to use sed or how to process your txt file with python?

    – Enix
    Nov 23 '18 at 3:06











  • Yes, I'm sorry, I was wrong, I just corrected now is my good question

    – F.Moab
    Nov 23 '18 at 3:27













  • you can use file.readline() or use iterator on file object to read line by line and use var count to count line no

    – Ravindhar Konka
    Nov 23 '18 at 4:52














1












1








1








I have a text file with 1 column of different values (floating and integer) and I would like to change a value according to its line. my code is as follows: but I did it knowing what value I should replace, how to do it according to the line?



with open('table1.txt', 'r+') as file:
text = file.read()
i = text.index('3.6') # 3.6 = old value
file.seek(0)
file.write(text[:i] + '7.84' + text[i + len('3.6'):]) # 7.84 = new value


Secondly, how can I choose a row when there are several columns in my file? For example, column 2 row 1? ? thanx










share|improve this question
















I have a text file with 1 column of different values (floating and integer) and I would like to change a value according to its line. my code is as follows: but I did it knowing what value I should replace, how to do it according to the line?



with open('table1.txt', 'r+') as file:
text = file.read()
i = text.index('3.6') # 3.6 = old value
file.seek(0)
file.write(text[:i] + '7.84' + text[i + len('3.6'):]) # 7.84 = new value


Secondly, how can I choose a row when there are several columns in my file? For example, column 2 row 1? ? thanx







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 4:36









eyllanesc

77.1k103156




77.1k103156










asked Nov 23 '18 at 2:58









F.MoabF.Moab

35




35













  • This is not a python question. Please move this question to unix.stackexchange.com or similar sites

    – Kevin Fang
    Nov 23 '18 at 3:06











  • so you are asking how to use sed or how to process your txt file with python?

    – Enix
    Nov 23 '18 at 3:06











  • Yes, I'm sorry, I was wrong, I just corrected now is my good question

    – F.Moab
    Nov 23 '18 at 3:27













  • you can use file.readline() or use iterator on file object to read line by line and use var count to count line no

    – Ravindhar Konka
    Nov 23 '18 at 4:52



















  • This is not a python question. Please move this question to unix.stackexchange.com or similar sites

    – Kevin Fang
    Nov 23 '18 at 3:06











  • so you are asking how to use sed or how to process your txt file with python?

    – Enix
    Nov 23 '18 at 3:06











  • Yes, I'm sorry, I was wrong, I just corrected now is my good question

    – F.Moab
    Nov 23 '18 at 3:27













  • you can use file.readline() or use iterator on file object to read line by line and use var count to count line no

    – Ravindhar Konka
    Nov 23 '18 at 4:52

















This is not a python question. Please move this question to unix.stackexchange.com or similar sites

– Kevin Fang
Nov 23 '18 at 3:06





This is not a python question. Please move this question to unix.stackexchange.com or similar sites

– Kevin Fang
Nov 23 '18 at 3:06













so you are asking how to use sed or how to process your txt file with python?

– Enix
Nov 23 '18 at 3:06





so you are asking how to use sed or how to process your txt file with python?

– Enix
Nov 23 '18 at 3:06













Yes, I'm sorry, I was wrong, I just corrected now is my good question

– F.Moab
Nov 23 '18 at 3:27







Yes, I'm sorry, I was wrong, I just corrected now is my good question

– F.Moab
Nov 23 '18 at 3:27















you can use file.readline() or use iterator on file object to read line by line and use var count to count line no

– Ravindhar Konka
Nov 23 '18 at 4:52





you can use file.readline() or use iterator on file object to read line by line and use var count to count line no

– Ravindhar Konka
Nov 23 '18 at 4:52












1 Answer
1






active

oldest

votes


















0














if columns in your file are splited by space:



def change_value(column, row, new_value):
lines =
with open('table1.txt', 'r+') as file:
for line in file:
lines.append(line.rstrip().split())

lines[row - 1][column - 1] = str(new_value)
file.seek(0)
for line in lines:
line[-1] += "n" # or "rn" on windows
file.write(' '.join(line))


change_value(2, 1, 7.84)





share|improve this answer


























  • Thank you very much it works very well :D ! But how to manage the spaces in file.write()? if for example I want after 3 written values it to return to the line. so that I can find the initial columns.

    – F.Moab
    Nov 24 '18 at 16:39













  • I'am not complitly understand your question. Pleas tell litle more. in that example, you can get any line in lines list. line contain values of columns just do lines[index_of_line_you_want][index_of_column_you_want]. indexes is numirated from 0, that why in example I get it like [index - 1] do all manipulations with data you want after file is readed and before it writen.

    – Andrey Suglobov
    Nov 24 '18 at 17:59













  • how to return to the line all 3 values? because here the code works well but modifies the text file by writing all the values in succession. example : change_value(2, 1, 7.84) --> 1.007825 7.84 0.5 0.95 4.002603 0 0.5 0.05 ... We no longer see the columns in the file

    – F.Moab
    Nov 24 '18 at 20:36













  • now your columns are separated by spaces. betwin the quotes in ' '.join(line) you can set a separator of columns. it may be two spaces or any you want. The symbol t will set space betwin column aqual the one tab, if you want two - set ttand so on... if you will set non-spaced symbol like - or other, you shoud set same in previos ....split("your_separator_here") method to corect read and divide values from line. By defalt it seted to space. Hope it helps.

    – Andrey Suglobov
    Nov 24 '18 at 22:51













  • Yes thank you so much ! but a other little problem, when I put row > 1 I have directly : change_value(2, 2, 7.84) File "run.py", line 7, in change_value lines[row - 1][column - 1] = str(new_value) IndexError: list index out of range

    – F.Moab
    Nov 25 '18 at 4:29













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440158%2fhow-to-change-the-value-of-a-line-in-specific-column-in-a-txt-file-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














if columns in your file are splited by space:



def change_value(column, row, new_value):
lines =
with open('table1.txt', 'r+') as file:
for line in file:
lines.append(line.rstrip().split())

lines[row - 1][column - 1] = str(new_value)
file.seek(0)
for line in lines:
line[-1] += "n" # or "rn" on windows
file.write(' '.join(line))


change_value(2, 1, 7.84)





share|improve this answer


























  • Thank you very much it works very well :D ! But how to manage the spaces in file.write()? if for example I want after 3 written values it to return to the line. so that I can find the initial columns.

    – F.Moab
    Nov 24 '18 at 16:39













  • I'am not complitly understand your question. Pleas tell litle more. in that example, you can get any line in lines list. line contain values of columns just do lines[index_of_line_you_want][index_of_column_you_want]. indexes is numirated from 0, that why in example I get it like [index - 1] do all manipulations with data you want after file is readed and before it writen.

    – Andrey Suglobov
    Nov 24 '18 at 17:59













  • how to return to the line all 3 values? because here the code works well but modifies the text file by writing all the values in succession. example : change_value(2, 1, 7.84) --> 1.007825 7.84 0.5 0.95 4.002603 0 0.5 0.05 ... We no longer see the columns in the file

    – F.Moab
    Nov 24 '18 at 20:36













  • now your columns are separated by spaces. betwin the quotes in ' '.join(line) you can set a separator of columns. it may be two spaces or any you want. The symbol t will set space betwin column aqual the one tab, if you want two - set ttand so on... if you will set non-spaced symbol like - or other, you shoud set same in previos ....split("your_separator_here") method to corect read and divide values from line. By defalt it seted to space. Hope it helps.

    – Andrey Suglobov
    Nov 24 '18 at 22:51













  • Yes thank you so much ! but a other little problem, when I put row > 1 I have directly : change_value(2, 2, 7.84) File "run.py", line 7, in change_value lines[row - 1][column - 1] = str(new_value) IndexError: list index out of range

    – F.Moab
    Nov 25 '18 at 4:29


















0














if columns in your file are splited by space:



def change_value(column, row, new_value):
lines =
with open('table1.txt', 'r+') as file:
for line in file:
lines.append(line.rstrip().split())

lines[row - 1][column - 1] = str(new_value)
file.seek(0)
for line in lines:
line[-1] += "n" # or "rn" on windows
file.write(' '.join(line))


change_value(2, 1, 7.84)





share|improve this answer


























  • Thank you very much it works very well :D ! But how to manage the spaces in file.write()? if for example I want after 3 written values it to return to the line. so that I can find the initial columns.

    – F.Moab
    Nov 24 '18 at 16:39













  • I'am not complitly understand your question. Pleas tell litle more. in that example, you can get any line in lines list. line contain values of columns just do lines[index_of_line_you_want][index_of_column_you_want]. indexes is numirated from 0, that why in example I get it like [index - 1] do all manipulations with data you want after file is readed and before it writen.

    – Andrey Suglobov
    Nov 24 '18 at 17:59













  • how to return to the line all 3 values? because here the code works well but modifies the text file by writing all the values in succession. example : change_value(2, 1, 7.84) --> 1.007825 7.84 0.5 0.95 4.002603 0 0.5 0.05 ... We no longer see the columns in the file

    – F.Moab
    Nov 24 '18 at 20:36













  • now your columns are separated by spaces. betwin the quotes in ' '.join(line) you can set a separator of columns. it may be two spaces or any you want. The symbol t will set space betwin column aqual the one tab, if you want two - set ttand so on... if you will set non-spaced symbol like - or other, you shoud set same in previos ....split("your_separator_here") method to corect read and divide values from line. By defalt it seted to space. Hope it helps.

    – Andrey Suglobov
    Nov 24 '18 at 22:51













  • Yes thank you so much ! but a other little problem, when I put row > 1 I have directly : change_value(2, 2, 7.84) File "run.py", line 7, in change_value lines[row - 1][column - 1] = str(new_value) IndexError: list index out of range

    – F.Moab
    Nov 25 '18 at 4:29
















0












0








0







if columns in your file are splited by space:



def change_value(column, row, new_value):
lines =
with open('table1.txt', 'r+') as file:
for line in file:
lines.append(line.rstrip().split())

lines[row - 1][column - 1] = str(new_value)
file.seek(0)
for line in lines:
line[-1] += "n" # or "rn" on windows
file.write(' '.join(line))


change_value(2, 1, 7.84)





share|improve this answer















if columns in your file are splited by space:



def change_value(column, row, new_value):
lines =
with open('table1.txt', 'r+') as file:
for line in file:
lines.append(line.rstrip().split())

lines[row - 1][column - 1] = str(new_value)
file.seek(0)
for line in lines:
line[-1] += "n" # or "rn" on windows
file.write(' '.join(line))


change_value(2, 1, 7.84)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 6:57

























answered Nov 23 '18 at 6:30









Andrey SuglobovAndrey Suglobov

1349




1349













  • Thank you very much it works very well :D ! But how to manage the spaces in file.write()? if for example I want after 3 written values it to return to the line. so that I can find the initial columns.

    – F.Moab
    Nov 24 '18 at 16:39













  • I'am not complitly understand your question. Pleas tell litle more. in that example, you can get any line in lines list. line contain values of columns just do lines[index_of_line_you_want][index_of_column_you_want]. indexes is numirated from 0, that why in example I get it like [index - 1] do all manipulations with data you want after file is readed and before it writen.

    – Andrey Suglobov
    Nov 24 '18 at 17:59













  • how to return to the line all 3 values? because here the code works well but modifies the text file by writing all the values in succession. example : change_value(2, 1, 7.84) --> 1.007825 7.84 0.5 0.95 4.002603 0 0.5 0.05 ... We no longer see the columns in the file

    – F.Moab
    Nov 24 '18 at 20:36













  • now your columns are separated by spaces. betwin the quotes in ' '.join(line) you can set a separator of columns. it may be two spaces or any you want. The symbol t will set space betwin column aqual the one tab, if you want two - set ttand so on... if you will set non-spaced symbol like - or other, you shoud set same in previos ....split("your_separator_here") method to corect read and divide values from line. By defalt it seted to space. Hope it helps.

    – Andrey Suglobov
    Nov 24 '18 at 22:51













  • Yes thank you so much ! but a other little problem, when I put row > 1 I have directly : change_value(2, 2, 7.84) File "run.py", line 7, in change_value lines[row - 1][column - 1] = str(new_value) IndexError: list index out of range

    – F.Moab
    Nov 25 '18 at 4:29





















  • Thank you very much it works very well :D ! But how to manage the spaces in file.write()? if for example I want after 3 written values it to return to the line. so that I can find the initial columns.

    – F.Moab
    Nov 24 '18 at 16:39













  • I'am not complitly understand your question. Pleas tell litle more. in that example, you can get any line in lines list. line contain values of columns just do lines[index_of_line_you_want][index_of_column_you_want]. indexes is numirated from 0, that why in example I get it like [index - 1] do all manipulations with data you want after file is readed and before it writen.

    – Andrey Suglobov
    Nov 24 '18 at 17:59













  • how to return to the line all 3 values? because here the code works well but modifies the text file by writing all the values in succession. example : change_value(2, 1, 7.84) --> 1.007825 7.84 0.5 0.95 4.002603 0 0.5 0.05 ... We no longer see the columns in the file

    – F.Moab
    Nov 24 '18 at 20:36













  • now your columns are separated by spaces. betwin the quotes in ' '.join(line) you can set a separator of columns. it may be two spaces or any you want. The symbol t will set space betwin column aqual the one tab, if you want two - set ttand so on... if you will set non-spaced symbol like - or other, you shoud set same in previos ....split("your_separator_here") method to corect read and divide values from line. By defalt it seted to space. Hope it helps.

    – Andrey Suglobov
    Nov 24 '18 at 22:51













  • Yes thank you so much ! but a other little problem, when I put row > 1 I have directly : change_value(2, 2, 7.84) File "run.py", line 7, in change_value lines[row - 1][column - 1] = str(new_value) IndexError: list index out of range

    – F.Moab
    Nov 25 '18 at 4:29



















Thank you very much it works very well :D ! But how to manage the spaces in file.write()? if for example I want after 3 written values it to return to the line. so that I can find the initial columns.

– F.Moab
Nov 24 '18 at 16:39







Thank you very much it works very well :D ! But how to manage the spaces in file.write()? if for example I want after 3 written values it to return to the line. so that I can find the initial columns.

– F.Moab
Nov 24 '18 at 16:39















I'am not complitly understand your question. Pleas tell litle more. in that example, you can get any line in lines list. line contain values of columns just do lines[index_of_line_you_want][index_of_column_you_want]. indexes is numirated from 0, that why in example I get it like [index - 1] do all manipulations with data you want after file is readed and before it writen.

– Andrey Suglobov
Nov 24 '18 at 17:59







I'am not complitly understand your question. Pleas tell litle more. in that example, you can get any line in lines list. line contain values of columns just do lines[index_of_line_you_want][index_of_column_you_want]. indexes is numirated from 0, that why in example I get it like [index - 1] do all manipulations with data you want after file is readed and before it writen.

– Andrey Suglobov
Nov 24 '18 at 17:59















how to return to the line all 3 values? because here the code works well but modifies the text file by writing all the values in succession. example : change_value(2, 1, 7.84) --> 1.007825 7.84 0.5 0.95 4.002603 0 0.5 0.05 ... We no longer see the columns in the file

– F.Moab
Nov 24 '18 at 20:36







how to return to the line all 3 values? because here the code works well but modifies the text file by writing all the values in succession. example : change_value(2, 1, 7.84) --> 1.007825 7.84 0.5 0.95 4.002603 0 0.5 0.05 ... We no longer see the columns in the file

– F.Moab
Nov 24 '18 at 20:36















now your columns are separated by spaces. betwin the quotes in ' '.join(line) you can set a separator of columns. it may be two spaces or any you want. The symbol t will set space betwin column aqual the one tab, if you want two - set ttand so on... if you will set non-spaced symbol like - or other, you shoud set same in previos ....split("your_separator_here") method to corect read and divide values from line. By defalt it seted to space. Hope it helps.

– Andrey Suglobov
Nov 24 '18 at 22:51







now your columns are separated by spaces. betwin the quotes in ' '.join(line) you can set a separator of columns. it may be two spaces or any you want. The symbol t will set space betwin column aqual the one tab, if you want two - set ttand so on... if you will set non-spaced symbol like - or other, you shoud set same in previos ....split("your_separator_here") method to corect read and divide values from line. By defalt it seted to space. Hope it helps.

– Andrey Suglobov
Nov 24 '18 at 22:51















Yes thank you so much ! but a other little problem, when I put row > 1 I have directly : change_value(2, 2, 7.84) File "run.py", line 7, in change_value lines[row - 1][column - 1] = str(new_value) IndexError: list index out of range

– F.Moab
Nov 25 '18 at 4:29







Yes thank you so much ! but a other little problem, when I put row > 1 I have directly : change_value(2, 2, 7.84) File "run.py", line 7, in change_value lines[row - 1][column - 1] = str(new_value) IndexError: list index out of range

– F.Moab
Nov 25 '18 at 4:29




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440158%2fhow-to-change-the-value-of-a-line-in-specific-column-in-a-txt-file-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python