I am trying to edit a text file in python 3.7. Basically, I have a text file (file_1.txt) that contains numbers - 3 columns and 5 rows like this one
1, 10, 20 2, 20, 30 3, 30, 50 4, 35, 60 5, 50, 100 I would like to edit that file in order to get something a little bit different, basically this
1, 10, 20 2, 20, 30 3, 30, 50 4, 35, 60 5, 50, 100 6, 10, 20 7, 20, 30 8, 30, 50 9, 35, 60 10, 50, 100 The second and third column are copied, and the first column is continuing with numbers, adding one each new line. I was trying to do this but I wasn't successful. Here is what I've tried:
with open("file_1.txt", "r+") as file1: file1.read() i = 6 imax = 10 while i <= imax: sentence = str(i) + "\n" file1.write(sentence) i = i + 1 I don't understand how to copy the second and third column.
Does anyone know how to do this?
line.split(',')will generate a vector with 3 index... To write with 3 columns : file1,write(firstcolumn + ', ' + secondcolumn + ', ' + thirdcolumn)