Suppose that I am having two text files 1.txt and 2.txt
The content of 1.txt
as:
[['Hi', 'I'], ['I'], ['_am']]
and
The content of 2.txt
as:
[['_a', 'good'], ['boy']]
How to join the same and write the same into a new file in time efficient manner, say 3.txt
, which should look like this:
[['Hi', 'I'], ['I'], ['_am'], ['_a', 'good'], ['boy']]
Note: I want the special characters (_) to be remain as it is.
I have tried from a previous answer of stack overflow which is mentioned in Concatenation of Text Files (.txt files) with list in python?
What I have tried is as follows:
global inputList
inputList = []
path = "F:/Try/"
def load_data():
for file in ['1.txt', '2.txt']:
with open(path + file, 'r', encoding = 'utf-8) as infile:
inputList.extend(infile.readlines())
print(inputList)
load_data()
But I am not getting the desired output as shown in above. The output what I am getting right now is as follows:
["[['Hi', 'I'], ['I'], ['_am']]", "[['_a', 'good'], ['boy']]"]
Why there is a extra (" ") in the current output of mine.
Please suggest something productive?
Thanks in Advance.