tags:

views:

136

answers:

8

Hi, I have a text file with lots of lines and with this structure:

[('name_1a',
'name_1b',
value_1),
('name_2a',
'name_2b',
value_2),
.....
.....
('name_XXXa',
'name_XXXb',
value_XXX)]

I would like to convert it to:

name_1a, name_1b, value_1
name_2a, name_2b, value_2
......
name_XXXa, name_XXXb, value_XXX

I wonder what would be the best way, whether awk, python or bash.

Thanks

Jose

+1  A: 

It looks like legal Python. You might be able to just import it as a module and then write it back out after formatting it.

Noufal Ibrahim
while it seems to be Python, it's not clear how you could import the content.
SilentGhost
After a bit of alteration. Say put a `foo=` in front of the whole thing perhaps?
Noufal Ibrahim
A: 

Awk is typically line oriented, and bash is a shell, with limited numbrer of string manipulation functions. It really depends on where your strength as a programmer lies, but all other things being equal, I would choose python.

Did you ever consider that by redirecting the time it took to post this on SO, you could have had it done?

"AWK is a language for processing files of text. A file is treated as a sequence of records, and by default each line is a record. Each line is broken up into a sequence of fields, so we can think of the first word in a line as the first field, the second word as the second field, and so on. An AWK program is of a sequence of pattern-action statements. AWK reads the input a line at a time. A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed." - Alfred V. Aho[2]

gahooa
+2  A: 

Tried evaluating it python? Looks like a list of tuples to me.

eval(your_string)

Note, it's massively unsafe! If there's code in there to delete your hard disk, evaluating it will run that code!

Oli
If your data starts as you say it does, you might want to prepend "data = " to it. You can do this dynamically in the python or edit the file. This means you'll be able to access it rather than just parse it into the ether.
Oli
A: 

Asking what's the best language for doing a given task is a very different question to say, asking: 'what's the best way of doing a given task in a particular language'. The first, what you're asking, is in most cases entirely subjective.

Since this is a fairly simple task, I would suggest going with what you know (unless you're doing this for learning purposes, which I doubt).
If you know any of the languages you suggested, go ahead and solve this in a matter of minutes. If you know none of them, now enters the subjective part, I would suggest learning Python, since it's so much more fun than the other 2 ;)

Idan K
If he was asking 'Python, perl or Ruby' I'd definitely agree with you. But awk, bash and Python are three languages that differ substantially in their capabilities and in what's convenient to do in them.
Omnifarious
+2  A: 

I would like to use Python:

lines = open('filename.txt','r').readlines()
n = len(lines) # n % 3 == 0
for i in range(0,n,3):
    name1 = lines[i].strip("',[]\n\r")
    name2 = lines[i+1].strip("',[]\n\r")
    value = lines[i+2].strip("',[]\n\r")
    print name1,name2,value
Yin Zhu
A: 

If the values are legal python values, you can take advantage of eval() since your data is a legal python data sucture. The following would work if values are integers, otherwise you might have to massage the print call a bit:

input = """[('name_1a',
          'name_1b',
          1),
         ('name_2a',
          'name_2b',
          2),
         ('name_XXXa',
          'name_XXXb',
          3)]"""

for e in eval(input):
    print '%s,%s,%d' % e

P.S. using eval() is quite controversial since it will execute any valid python code that you pass into it, so take care.

liwp
+1  A: 

Oh boy, here is a job for ast.literal_eval: (literal_eval is safer than eval, since it restricts the input string to literals such as strings, numbers, tuples, lists, dicts, booleans and None:

import ast
filename='in'
with open(filename,'r') as f:
    contents=f.read()
    data=ast.literal_eval(contents)

for elt in data:
    print(', '.join(map(str,elt)))
unutbu
+1  A: 

here's one way to do it with (g)awk

$ awk -vRS=")," ' { gsub(/\n|[\047\]\[)(]/,"") } 1' file
name_1a,name_1b,value_1
name_2a,name_2b,value_2
name_XXXa,name_XXXb,value_XXX
ghostdog74