Output values to rows Different delimiters

Posted on 2014-04-05
Medium Priority
Last Modified: 2014-04-06
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

Question by:jay_eire
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 29

Accepted Solution

pepr earned 2000 total points
ID: 39981604
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


Author Closing Comment

ID: 39981711
Thank you very much for the assistance.

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question