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

4
  • Is the spacing of the 1,2,3, etc. always consistent? Commented Mar 7, 2018 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. Commented Mar 7, 2018 at 15:18
  • Possible duplicate of How do I read a file line-by-line into a list? Commented Mar 7, 2018 at 17:03
  • @chrisz yes, the spacing is consistent Commented Mar 7, 2018 at 18:14

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.