Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Output values to rows Different delimiters

Posted on 2014-04-05
2
Medium Priority
?
229 Views
Last Modified: 2014-04-06
Hi,
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

0
Comment
Question by:jay_eire
[X]
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
2 Comments
 
LVL 29

Accepted Solution

by:
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:
a.py
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):
c:\_Python\jay_eire\Q_28405839>a.py
{"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

0
 
LVL 7

Author Closing Comment

by:jay_eire
ID: 39981711
Thank you very much for the assistance.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

609 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