Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Output values to rows

Posted on 2014-04-03
2
Medium Priority
?
170 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
[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 16

Accepted Solution

by:
Walter Ritzel earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 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…
Suggested Courses

670 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