Hello I am having trouble getting my Python to work correctly. I have a text file of the following format:
3|1|15382|25364||
3|2|15384|25364||
43|1|27444|27297|4849569|
43|2|27486|27297|4329265|
107|2|24940|684269||
115|1|24869|684269||
My current Python code:
f1 = open('myFile.txt', 'r')
f2 = open('myFileOutput.txt', 'w')
for line in f1:
f2.write(line.replace('|\n', ','))
f1.close()
f2.close()
My current code will create a myFileOutput.txt that looks like this:
3|1|15382|25364|,3|2|15384|25364|,43|1|27444|27297|4849569,43|2|27486|27297|4329265,107|2|24940|684269|,115|1|24869|684269||
What I want is when there is no data between two || it should keep the second |. My current code doesn’t do that. Here is an example of what I want it to look like:
3|1|15382|25364||,3|2|15384|25364||,43|1|27444|27297|4849569,43|2|27486|27297|4329265,107|2|24940|684269||,115|1|24869|684269||
I am using Python 2.7.3 and also these files can get up to 500MB in size.
Use
f2.write(line.replace('|\n', ',').replace('|,', '||,'))