Solved

Output values to rows Different delimiters

Posted on 2014-04-05
2
227 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

724 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