0

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?

8
  • It looks like you're just appending data to the file. Is that what you want to know, or will the "edits" involve other things like changing existing lines or deleting some of them. Commented Jan 30, 2019 at 21:25
  • You need to split the lines to get the columns. line.split(',') will generate a vector with 3 index... To write with 3 columns : file1,write(firstcolumn + ', ' + secondcolumn + ', ' + thirdcolumn) Commented Jan 30, 2019 at 21:30
  • I was trying to split the line, but then I got strings and I don't know how to make the first column with that, also there were problems with adding those values to new list Commented Jan 30, 2019 at 21:34
  • I don't care if I got a new file (file_2.txt for example) with these 10 rows and 3 columns, or I append additional 5 rows to existing file. Commented Jan 30, 2019 at 21:38
  • @CroSimpson2.0 Please see my answer below. Commented Jan 30, 2019 at 21:39

8 Answers 8

2

If this is a csv-like file, you may want to use pandas (which is one of the best ways for dataframe manipulation). A quick example:

import pandas as pd df = pd.read_csv("<path_to_data_file>", header=None) df = pd.concat([df, df]) df[0] = list(range(1, 11)) df.to_csv("result.csv", header=None, index=None) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thx for a suggestion. If you know any other way without this module, I would like to see it.
@CroSimpson2.0 There are a couple of working solutions without pandas below. However, pandas is one of the most powerful (and standard) libs (along with numpy, scipy, etc.) for data pipeline and you should definitely try to use it, as it is way simpler to use.
0

Script below will build new file and you can set number of lines that you want to create.

First all lines are read from input file and then it writes number of lines you set to new file.

list_emitter can infinitely yield items from given list, so you can just adjust output_lines_count variable to make your output file even bigger.

def list_emitter(l): """This generator will endlessly yield items from given list.""" while True: for item in l: yield item with open('file_1.txt') as input_file: lines = input_file.readlines() # Create list of lines with open('output_file.txt', 'w') as output_file: output_lines_count = 10 # Set how many output lines you want for counter, line in enumerate(list_emitter(lines)): if counter == output_lines_count: break first, second, third = line.strip().split() # Parse line output_file.write('{}, {} {}\n'.format(counter+1, second, third)) 

Comments

0

Pythonic-way: It'll append the newlines to the file.

with open('sample.txt', 'r') as f: l = [i.strip() for i in f.readlines()] max_row = int(l[-1].split(',')[0]) x = [str(i) for i in range(max_row+1,11)] y = [i.split(',', 1)[-1] for i in l] with open('sample.txt', 'a') as f: for item in [x[i]+',' + y[i] for i in range(len(x))]: f.write("%s\n" % item) 

PS: max row can be length of the number of lines

Comments

0

Another way:

with open("test.txt", "r+") as file1: lines = file1.readlines() index = 0 i = 6 imax = 10 while i <= imax: sentence = lines[index].split(", ")[1:] sentence.insert(0, str(i)) file1.write(", ".join(sentence)) i += 1 index += 1 

Output:

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 

Comments

0

First, you need to read all data from the input, and store it.

Then go through it again and write it to the file.

data = [] with open("file_1.txt", "r+") as file1: # read the data for line in file1: # .strip() to remove the newline # .split(", ") to split into 3 values # map(int, ...) to convert each from string to integer index, column2, column3 = map(int, line.strip().split(", ")) #save the second and third coluumn data.append((column2, column3)) # now write it back again: for column2, column3 in data: index += 1 # continue incrementing the index # format the lines and write them into the file file1.write("{}, {}, {}\n".format(index, column2, column3)) 

Comments

0

This module also works:

def edit(nrows, filename): nrows +=1 #to avoid off-by-one error because dealing with lists outf = open(filename, 'a') column_1 = [1, 2, 3, 4, 5] column_2 = [10, 20, 30, 35, 50] column_3 = [20, 30, 50, 60, 100] last_column_1 = column_1[-1] list_1 = list(range(last_column_1+1, last_column_1+nrows)) list_2 = nrows//len(column_2)*column_2 + column_2[0:nrows%len(column_2)] list_3 = nrows//len(column_3)*column_3 + column_3[0:nrows%len(column_3)] for c1, c2, c3 in zip(list_1, list_2, list_3): outf.write("{}, {}, {}\n".format(c1, c2, c3)) if __name__ == '__main__': edit(10, 'file.txt') 

Assuming there is a file.txt with text:

1, 10, 20 2, 20, 30 3, 30, 50 4, 35, 60 5, 50, 100 

Comments

0

Short and easy. Just 3 lines.

with open('file_1.txt', 'r+') as f: for num, content in enumerate(f.readlines()): f.write(f'{num+6}, {content[3:]}') 

Comments

0

This approach works directly with each line as a string, without splitting any more columns than necessary.

The first for loop extacts Cols 2&3 (with leading comma) into a List, tracking the line count. The second loop appends this list incrementing index starting with the count.

with open("file_1.txt", "r+") as file1: our_data = [] count = 0 for line in file1: first_comma_pos = line.find(',') # extract cols 2&3 including the leading comma our_data.append(line[first_comma_pos:]) count += 1 for i in range(count): sentence = str(i + count) + our_data[i] + '\n' file1.write(sentence) 

2 Comments

Hi Alex! I was going throughout your code and I can't fully understand it. Can you please tell me why did you write colon at the line where you were using find method ?
@CroSimpson2.0 sorry for the opaque code, hopefully this new edit is a bit better. str[a:] will return a substring starting with the a'th character until the end of the string (ex: 'hello'[3:] # returns 'lo'), so I've used this to extract Cols 2 & 3 by finding the first comma position

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.