Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to split a string in Python 3, get 'str' does not support buffer interface

So I'm trying to take data from a website and parse it into an object. The data is separated by vertical bars ("|"). However, when I split my string using .split('|'), I get

TypeError: 'str' does not support the buffer interface

I am still attempting to learn Python. I have done some digging on this error but every example I could find related to sending or receiving data and there was a lot of jargon I was unfamiliar with. One solution said to use .split(b'|'), but then this somehow turned my string into a byte and prevented me from printing it in the final line.

Below is my code. Any help you can offer would be greatly appreciated!

with urllib.request.urlopen('ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqtraded.txt') as response:
    html = response.read()
rawStockList = html.splitlines()

stockList = []
for i in rawStockList:
    stockInfo = i.split('|')
    try:
        stockList.append(Stock(stockInfo[1], stockInfo[2], stockInfo[5], stockInfo[10], 0))
    except IndexError:
        pass
print([str(item) for item in stockList])
like image 588
catsitter1029 Avatar asked Feb 09 '23 02:02

catsitter1029


1 Answers

The items inside rawStockList is actually of the type byte, as response.read() returns that since it doesn't necessary know the encoding. You need to turn that into a proper string by decoding it with an encoding. Assuming the files are encoded in utf8, you need something like:

for i in rawStockList:
    stockInfo = i.decode('utf8').split('|')
like image 57
metatoaster Avatar answered Feb 12 '23 02:02

metatoaster