I am having a text file in which data is in this form:
Before formatting:
a1 ,a2,a3:b1 ,b2,b3:c1 ,c2,c3:....so on.
Now i want to convert the data in this particular form:
After formatting:
a1,a2,a3
b1,b2,b3
c1,c2,c3
...so on
To do: first i want to convert :
char in 'newline' and want to recover the space after every first character such as a1 b1 c1
. the space is creating problem as the data after formatting is to be converted into csv file.
I tried transferring one by one char from one file to other in which i used an if case to replace :
to 'newline'.
For newline I used this \n
file.txt
is file which contain a1 ,a2,a3:b1 ,b2,b3:c1 ,c2,c3:....so on
convert.txt
if file in which i want formatted data
with open('file.txt','r') as rf:
with open('convert.txt','w') as wf:
a=1
rf_temp = rf.read(a)
while len(rf_temp) > 0:
if rf_temp == ':':
rf_temp.replace(':','\n')
wf.write(rf_temp)
rf_temp = rf.read(a)
wf.close()
rf.close
the data gets copied clearly but not in format which i want including ':' this symbol. There are no issues in copying.
# define the input file and output file
inputFile = 'test.txt'
outFile = "test.csv"
# read data from input file
data = open(inputFile).read()
# change data to wanted format, using '\n' to replace ':'
data = "\n".join(data.split(':'))
# save the changed data to output file
outF = open(outFile, 'w')
outF.write(data)
print(data)
output
a1 ,a2,a3
b1 ,b2,b3
c1 ,c2,c3