0

I would have a huge list of Nasdaq stocks defined as a variable. Here's the code:

from ftplib import FTP

def my_function(data):
    print(data.decode())

ftp = FTP('ftp.nasdaqtrader.com')
ftp.login()
nasdaq=ftp.retrbinary('RETR /SymbolDirectory/nasdaqlisted.txt', my_function)

If I print out the string "nasdaq" I can see it comes in the following format:

AAAP|Advanced Accelerator Applications S.A. - American Depositary Shares|Q|N|N|100|N|N
AAL|American Airlines Group, Inc. - Common Stock|Q|N|N|100|N|N
AAME|Atlantic American Corporation - Common Stock|G|N|N|100|N|N
AAOI|Applied Optoelectronics, Inc. - Common Stock|G|N|N|100|N|N
and so on...

I want a list where only the tickers are printed out:

AAAP
AAL
AAME
AAOI

What is the right approach to do this?

Rafael Martínez
  • 335
  • 1
  • 2
  • 17
  • What is your current approach to do it? Why do you think it's wrong? – jonrsharpe Jan 16 '17 at 00:09
  • You could use the string `split()` method at pipes ("|"), which will return a list. The first item in the list should be the ticker – jkr Jan 16 '17 at 00:12
  • @TigerhawkT3 Why would my post be a duplicate? The post you mention does extract a substring from a bigger string but doesn't relate to having to loop on more lines. – Rafael Martínez Jan 16 '17 at 01:34
  • Have you considered the fact that you can split on a delimiter of a newline character? – TigerhawkT3 Jan 16 '17 at 01:45
  • Yes, I can get the substring in between '\r\n' (new line) and "|" (goes after every ticker). What makes me struggle is the repeating process, I'm not sure how to loop this to get the substring of each line. And I think your post doesn't mention that @TigerhawkT3 – Rafael Martínez Jan 16 '17 at 02:02
  • By "What makes me struggle is the repeating process" do you mean that you're unfamiliar with loops entirely? – TigerhawkT3 Jan 16 '17 at 02:07
  • Not entirely, I'm just a begginer who still struggles on how to apply theory in real problems. I hope there is something I can do to enhance my question because I really don't know where to get more useful insight. @TigerhawkT3 – Rafael Martínez Jan 16 '17 at 02:17
  • It sounds like you need to brush up with a tutorial. I recommend the [official Python tutorial](https://docs.python.org/3.6/tutorial/index.html). – TigerhawkT3 Jan 16 '17 at 02:36
  • I'll do that. Thanks for answering :) @TigerhawkT3 – Rafael Martínez Jan 16 '17 at 02:47

1 Answers1

0
print '\n'.join(share.split('|')[0] for share in nasdaq.splitlines())
Simon Fromme
  • 3,104
  • 18
  • 30
  • OP said it's a "huge" list, so processing the whole thing at once probably isn't a great plan. – TigerhawkT3 Jan 16 '17 at 00:26
  • "huge" is 3291 lines, that should not be too worrisome. But I agree if the file might be bigger. – Simon Fromme Jan 16 '17 at 00:28
  • 1
    Please edit your answer, or you could be down-voted. As it is now, it's just a suggestion with no context. Please see here for [How to Write a Good Answer](http://stackoverflow.com/help/how-to-answer). – jacefarm Jan 16 '17 at 00:41