Solved

Output values to rows

Posted on 2014-04-03
2
158 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

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…
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 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 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…

708 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

19 Experts available now in Live!

Get 1:1 Help Now