Solved

Output values to rows Different delimiters

Posted on 2014-04-05
2
222 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
2 Comments
 
LVL 28

Accepted Solution

by:
pepr earned 500 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now