0

I need to convert a list of ticker symbols: T GOOG KO PEP as examples in a a text file to a python list: ['T','GOOG','KO',PEP']. However, the current code I'm using keeps adding a space after each symbol yielding: ['T ','GOOG ','KO ',PEP '] instead. How can I get the tickers without spaces?

stocks = open('C:\Model\Stocks\official.txt', 'r').read()
print stocks.split('\n')
user1526586
  • 93
  • 1
  • 3
  • 10
  • Apparently the bimbos at Standard & Poors decided to put a nice little space after each symbol on the excel file. I guess I need to figure out how to remove that. – user1526586 Jul 16 '12 at 09:53

2 Answers2

0

I suggest strip function, which maps the result:

>>> a = ["A ", "B ", "C "]
>>> a
['A ', 'B ', 'C ']
>>> a = map(lambda x: x.strip(), a)
>>> a
['A', 'B', 'C']

In your example:

a = stocks.split('\n')
Gandi
  • 3,522
  • 2
  • 21
  • 31
0

Here you go.

stocks = open('C:\Model\Stocks\official.txt', 'r').read()
lstStripped =  [x.strip() for x in stocks.split('\n')]
print lstStripped 
Vinayak Kolagi
  • 1,831
  • 1
  • 13
  • 26