Output values to rows Different delimiters

I got some help with this issue before, my input file format has changed.

Its structure is like so {"pc2", "pc", "server"}{"AA01", "BD12", "TTH87"}{"Floor 3", "text text text text", " Floor 12"}{11,15,16}{A33,Z33,C11}

 I want to output my txt file into rows so it looks like this
pc2, AA01, Floor 3 ,11, A33
pc, BD12,  text text text text, 15, Z33,
server, TTH87, Floor 12, 16, C11

Whats different is in the first three sections the text is delimited by  quotes a comma and a space for example {"pc2", "pc", "server"} and the last last two sections the text is separated by just commas for example {11,15,16}

how can i adapt the script to do this?

filename = '28404749.csv'
f = open(filename,'rt')
for line in f.readlines():
		the objective of the line below is to transform this line in a matrix 2x2. see steps below
		for each line in the file:
			- replace every ocurrence of }{ by ]^[ -> to start the delimiters of the inner dimension;
			- replace every ocurrence of { by [[ -> to have the beggining of the outer dimension and beggining of the first inner dimension;
			- replace every ocurrence of } by ]] -> to have the ending of the outer dimension and ending of the last inner dimension;
			- transform all content in strings (that is the only thing I did not like it) -> using comma to find each element of the inner dimensions;
			- replacing the ^ by a , -> create a well formed array
			- using eval to evaluate the string into a well formed matrix
			- replace ' followed by space by only ' --> remove extra left space on some items
			- transposing  the matrix and eliminating ragged elements using the zip function and * 
	cleanArr = zip(*eval(line.replace("}{","']^['").replace("{","[['").replace("}","']]").replace(",","','").replace(']^[','],[').replace("' ","'")))
	for item in cleanArr:
		print item

Open in new window

Who is Participating?

Improve company productivity with a Business Account.Sign Up

peprConnect With a Mentor Commented:
I suggest another approach instead of the eval() and the serie of replace(). You can use the regular expression for a separator to split the string to the list of separated parts. Then each part can be split by comma, and the str.strip() with nondefault argument can be used to get rid of double quotes and spaces:
import re

rexSep = re.compile(r'{|}{|}')  # separators of the sublists

filename = '28404749.csv'
f = open(filename, 'rt')
for line in f:
    print line
    lst1 = rexSep.split(line)   # contains unwanted elements before the first {
    lst2 = lst1[1:-1]           # and after the last }. Get only the middle.
    llst = []                   # init for building a list of lists
    for part in lst2:
        # Split each part by comma and strip the spaces and double quotes.
        # Append the result to the list of lists
        llst.append([e.strip('" ') for e in part.split(',')])

    # The *llst expands the list of lists as if you wrote the inner
    # lists as the arguments of the zip().
    for t in zip(*llst):
        print ', '.join(t)

f.close()    # It is always better to close the file.

Open in new window

It prints for your input (the firs line wrapped):
{"pc2", "pc", "server"}{"AA01", "BD12", "TTH87"}{"Floor 3", 
"text text text text", " Floor 12"}{11,15,16}{A33,Z33,C11}
pc2, AA01, Floor 3, 11, A33
pc, BD12, text text text text, 15, Z33
server, TTH87, Floor 12, 16, C11

Open in new window

jay_eireAuthor Commented:
Thank you very much for the assistance.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.