tags:

views:

86

answers:

1

I have a list of horizontal names that is too long to open in excel. It's 90,000 names long. I need to add a comma after each name to put into my program. I tried find/replace but it freezes up my computer and crashes. Is there a clever way I can get a comma at the end of each name? My options to work with are python and excel thanks.

+5  A: 

If you actually had a Python list, say names, then ','.join(names) would make into a string with a comma between each name and the following one (if you need one at the end as well, just use + ',' to append one more comma to the result).

Even though you say you have "a list" I suspect you actually have a string instead, for example in a file, where the names are separated by...? You don't tell us, and therefore force us to guess. For example, if they're separated by line-ends (one name per line), your life is easiest:

with open('yourfile.txt') as f:
  result = ','.join(f)

(again, supplement this with a + ',' after the join if you need that, of course). That's because separation by line-ends is the normal default behavior for a text file, of course.

If the separator is something different, you'll have to read the file's contents as a string (with f.read()) and split it up appropriately then join it up again with commas.

For example, if the separator is a tab character:

with open('yourfile.txt') as f:
  result = ','.join(f.read().split('\t'))

As you see, it's not so much worse;-).

Alex Martelli
My guess is that it's space or tab separated. Only because he said: "horizontal list"
Atømix
Yeah its just sepearated by spaces
Robert A. Fettikowski
@Robert, then split by space (or, if by "spaces" you mean "arbitrary whitespace" instead, just call split without arguments, which will split the string by arbitrary sequences of whitespace -- spaces, tabs, and so on).
Alex Martelli
Thanks. Working now! Great help Alex.
Robert A. Fettikowski
@Robert, you're welcome!
Alex Martelli