0

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

Homie10
  • 13
  • 3
  • Is the spacing of the 1,2,3, etc. always consistent? – user3483203 Mar 07 '18 at 15:09
  • Why are you trying to open and read the .txt file as a .csv one? It would be better if you treat it as a common text file and then read it line by line, creating the array you want to write in the csv. – Aristotelis Mar 07 '18 at 15:18
  • Possible duplicate of [How do I read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-do-i-read-a-file-line-by-line-into-a-list) – baduker Mar 07 '18 at 17:03
  • @chrisz yes, the spacing is consistent – Homie10 Mar 07 '18 at 18:14

1 Answers1

1

This file is not well suited to be read by csv. Instead treat it as a regular text file.

array = []

with open('test.txt', 'r') as file_contents:
    for line in file_contents:
        # remove newlines, (), split on comma
        lsplit = line.strip().strip('()').split(',')
        # strip again to remove leading/trailing whitespace
        array.extend(map(str.strip, lsplit))

print(array)
#['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']

Then you can write the contents of this array as you wish. For example, if you wanted the format you showed above.

header = ['a','b','c','d','e','f','g','h','i']
with open('file.csv', 'w') as file_out:
    file_out.write(",".join(header) + "\n")  # write the header
    for i in range(len(array)//len(header)):
        file_out.write(",".join(array[i*len(header):(i+1)*len(header)]) + "\n")
pault
  • 41,343
  • 15
  • 107
  • 149
  • I added + "\n" to the line file_out.write(",".join(header)) and it prints out perfectly the way I wanted to. Thank you very much. – Homie10 Mar 07 '18 at 17:01