Solved

Output values to rows

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Python script with password security? 15 132
How can I iterate through mysql tables to alter character set? 3 82
Python Regex Problem 24 147
Python 3.5: SQL Results to CSV dump 8 81
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…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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…

756 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