I have a text file that looks like this
1
(2.10, 3)
(4, 5)
(6, 7)
(2, 2)
2
(2.10, 3)
(4, 5)
(6, 7)
(2, 2)
3
(6, 7)
(2, 2)
(30.2, 342)
(6, 7)
I want to read the txt file and create a csv file in this format with header and remove the brackets
a,b,c,d,e,f,g,h,i
1,2.10,3,4,5,6,7,2,2
2,2.10,3,4,5,6,7,2,2
3,6,7,2,2,30.2,342,6,7
Here's the code
import csv
import re
with open('test.txt', 'r') as csvfile:
csvReader = csv.reader(csvfile)
data = re.findall(r"\S+", csvfile.read())
array = []
array.append(data)
print (array)
file2 = open("file.csv", 'w')
writer = csv.writer(file2)
writer.writerows(array)
The output
1,"(2.10,",3),"(4,",5),"(6,",7),"(2,",2),2,"(2.10,",3),"(4,",5),"(6,",7),"(2,",2),3,"(6,",7),"(2,",2),"(30.2,",342),"(6,",7)
I tried removing the bracket using
array.append(str(data).strip('()'))
but no luck