2

I'm trying to create an array of stock tickers in Python 2.7 from a txt file. The txt file simply has 1 ticker per line like:

SRCE
ABTX
AMBC
ATAX

The code I'm using looks like:

FinTick= []

    def parseRus():
        try:
        readFile=open(r'filename.txt','r').read()
        splitFile=readFile.split('\n')
        FinTick.append(splitFile)
        print FinTick



        except Exception, e:
        print str(e)

When I call 'parseRus()' I get an output that looks like:

'\xff\xfeS\x00R\x00C\x00E\x00\r\x00', '\x00A\x00B\x00T\x00X\x00\r\x00', '\x00A\x00M\x00B\x00C\x00\r\x00', '\x00A\x00T\x00A\x00X\x00\r\x00'

The correct letters are present but not printing in plane text. I've used a couple other logic methods to populate the array but still get the same output format.

3
  • 1
    Have a look at numpy.loadtxt. This should work better than plain opening and reading. Commented Oct 6, 2016 at 13:56
  • What happens if you remove the r from your r'filename.txt'open? Commented Oct 6, 2016 at 13:59
  • @Chris_Rands I get an invalid mode or filename error. Commented Oct 6, 2016 at 14:12

2 Answers 2

2
>>> tickers = []
>>> with open("filename.txt", "r") as f:
        for ticker in f.readlines():
            tickers.append(ticker.strip())


>>> tickers
['SRCE', 'ABTX', 'AMBC', 'ATAX']

Try using readlines() and strip() instead.

Edit: Some clarity around f.readlines() and strip():

>>> with open("filename.txt", "r") as f:
        print(f.readlines())

['SRCE\n', 'ABTX\n', 'AMBC\n', 'ATAX']

So, when we're iterating through the list object returned by f.readlines(), we will need to strip the newline \n characters. Use the strip() method for str types to do this.

Edit 2: @Eli is right. We can just use for ticker in f instead of for ticker in f.readlines(), too.

>>> tickers = []
>>> with open("filename.txt", "r") as f:
        for ticker in f:
            tickers.append(ticker.strip())

>>> tickers
['SRCE', 'ABTX', 'AMBC', 'ATAX']
Sign up to request clarification or add additional context in comments.

7 Comments

No need to use readlines. Just do for ticker in f:. There is no need to read the entire file into RAM.
I'm not clear you've solved their issue, can you replicate the OP's current output?
OP's code actually works for me. I get the same result from their code as I do with my code. I just wonder if there are extraneous characters in their text file.
@Eli Korvigo I'm still getting the output in the same format. Could it have something to do with the 'r' preceding the 'filename.txt' input? I'm not sure why I'm getting the 'invalid mode or filename' error if I don't put the 'r' first.
Any ideas on why you get the right output and my output is formatted in such an odd fashion?
|
0

I finally figured a fix.

I had to modify the text file from 1 column to 1 row then saved as a .csv and modified the code to:

FinTick= []

def parseRus():
    try:
        readFile=open(r'filename.csv','r').read()
        splitFile=readFile.split(',')
        for eachLine in splitFile:
            splitLine=eachLine.split(',')
            ticker=splitLine[0]
            FinTick.append(ticker.strip())



    except Exception, e:
    print str(e)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.