You are not logged in.

#1 2005-07-27 08:32:05

bistrototal
Member
From: Norway
Registered: 2005-05-25
Posts: 59

[SOLVED] Removing brackets from file - python/sed

I wrote a small python-script in order to do some manipulation of columns. I'm quite unexperienced, and i stumbled upon a problem i didn't manage to solve:

When the script is finished with the data manipulation, everything is stored as data type "list", which implies the following form when i output the data with print: ['2321321', '321321321', '55555']. I don't want the brackets and ' (since i want to import the data into a spreadsheet). I've tried to make a loop to print out list content, but then i get everything in one column, which i don't want. (Something like: for l in line: print l )

Is there a way to print lists without the brackets and '? I tried to solve it with sed, but my knowledge of that program is zero, and no success. My solution was to open the output in a text editor and use search and replace, which is very tedious work for 50 files.

Offline

#2 2005-07-27 10:22:33

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: [SOLVED] Removing brackets from file - python/sed

I *know* there's a better way! I just cant remember it right now, but in the meantime this should work.

list = ["some","cool","list","of","stuff"]

biglist = ""

for item in list:
  biglist = biglist + item

print biglist

Offline

#3 2005-07-27 12:27:08

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: [SOLVED] Removing brackets from file - python/sed

I think the quicker way is using the join() method:

[andyr@roo ~]$ python
Python 2.4.1 (#1, Apr  5 2005, 11:00:51)
[GCC 3.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> items = ["one", "two", "three"]
>>> allitems = " ".join(items)
>>> print allitems
one two three

Of course, you could have "," or "t" (tabs), etc to delimit the columns rather than the space in the above example.

Offline

#4 2005-07-27 12:33:59

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: [SOLVED] Removing brackets from file - python/sed

arooaroo wrote:

I think the quicker way is using the join() method:

Of course, you could have "," or "t" (tabs), etc to delimit the columns rather than the space in the above example.

damn! i knew there was! i just couldnt remember, and was too lazy to read the docos.

iphitus

Offline

#5 2005-07-27 13:45:56

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: [SOLVED] Removing brackets from file - python/sed

yeah. and in addition, you can suppress newlines with print, by adding a comma after the statement, if I remember correctly..
print i,


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#6 2005-07-27 21:06:39

bistrototal
Member
From: Norway
Registered: 2005-05-25
Posts: 59

Re: [SOLVED] Removing brackets from file - python/sed

Thanks, folks!
I haven't tried it yet, but i'm sure this is what i need!

EDIT: Now i have - perfect!

Offline

Board footer

Powered by FluxBB