I'm trying to read a number of ticker symbols from a text file, but seem to have a textcoding issue.
This is the contents of a test file 'tickers.txt':
SPG
WBA
This is my testcode:
f = open("tickers.txt", 'r')
for ticker in f:
t = ticker.strip()
if t:
try:
print(">"+t+"<" + ' length = '+ str(len(t)))
i = 0
while i < len(t):
print(t[i])
i += 1
print('End')
except ValueError:
print('ValueError ticker')
And this is the resulting output:
>SPG< length = 4
S
P
G
End
>WBA< length = 3
W
B
A
End
For some reason there is an extra character in the first ticker symbol, which does not show when printed. Have read through several Q&A's here on StackOverflow I now assume it is a text coding issue, but don't understand yet how to solve this.... Do I need to add an 'encoding' statement to the file open command ? If so, which one ? How to detect ?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…