Skip to content
Advertisement

Store the contents of text file in return variable and manipulate wherever required in python?

I have below function & I am trying to get/store the contents of text file in another temp file(removing unnecessary line) with appending special character. But I also want the same content which is in temp text file with different special character next time but I am not able to do that.Below function is creating a temp file.To get desired output I need to create file every time with same function again which is not good way.Is there anything we can do without creating a temp/extra file and store the contents in return variable and append the special character whatever we want multiple times

JavaScript

file abc_12.txt

JavaScript

I want the contents of abc_12.txt file I can get in return something like that

JavaScript

and manipulate them wherever I want similar to below output

Output 1:

JavaScript

Output 2:

JavaScript

Advertisement

Answer

Maybe first you should read file, search names and keep on list

JavaScript

And later you can use this list to create temp file or other file

JavaScript

EDIT:

Minimal working code.

I used io.StringIO only to simulate file in memory – so everyone can simply copy and test it.

JavaScript

EDIT:

If you don’t want to save in file then you still need for-loop to create string

JavaScript

or to create new list (and eventually string)

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement