Solved

Output values to rows

Posted on 2014-04-03
2
162 Views
Last Modified: 2014-04-04
Hi, I am not sure if this is possible I have a txt file that is formatted as follows
{11,15,16,18}{pc2, pc, server}{AA01, BD12, TTH87}

The data is in three sections delimited by {} and each entry separated by a comma. The first entry in each section is related to each other.  I am looking to output my txt file into rows so it looks like this

11 pc2 AA01
15 pc BD12
16 server TTH87

How would I modify the code to do this?
f = open(filename,'rt')
a = [[int(token) for token in line.split()] for line in f.readlines()[::2]]
0
Comment
Question by:jay_eire
2 Comments
 
LVL 15

Accepted Solution

by:
Walter Ritzel earned 500 total points
ID: 39976968
I dont know if is the most smart or pythonic code, but here it is:
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


Note that line starting with cleanArr should be just one line in your code
0
 
LVL 7

Author Closing Comment

by:jay_eire
ID: 39977824
Awesome thanks, there is some really powerful features in python.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now