2
Write the function símbolo()
who accepts a string
(the name of a file: Nasdaq.txt) as input.
The archive will have company names and stock symbols. In this file, a company name will occupy a line and its action symbol will be on the next line. After this line will be a line with another company name and so on. Your program will read the file and store the name and symbol of the action in a dictionary.
file (part of it):
ACTIVISION INC
ATVI
ADOBE SYS INC
ADBE
ALTERA CORP
ALTR
AMAZON
AMZN
AMERICAN POWER CONVER CORP
APCC
AMGEN
AMGN
APOLLO GROUP-A
APOL
full file: https://easyupload.io/x6v37a
What I did:
def simbolo(arquivo):
empresas = {}
with open("nasdaq.txt") as f:
texto = f.read()
for i in range(len(texto.split("\n")) - 1) :
empresas.setdefault(texto.split("\n")[i],texto.split("\n")[i+1])
return empresas
empresas = simbolo("nasdaq.txt")
print(empresas)
The problem is that the dictionary is getting wrong with several " t":
{'ACTIVISION INC \t': 'ATVI', 'ATVI': 'ADOBE SYS INC', 'ADOBE SYS INC': 'ADBE', 'ADBE': 'ALTERA CORP \t', 'ALTERA CORP \t': 'ALTR', 'ALTR': 'AMAZON \t', 'AMAZON \t': 'AMZN', 'AMZN': 'AMERICAN POWER CONVER CORP \t', 'AMERICAN POWER CONVER CORP \t': 'APCC', 'APCC': 'AMGEN \t', 'AMGEN \t': 'AMGN', 'AMGN': 'APOLLO GROUP-A \t', 'APOLLO GROUP-A \t': 'APOL', 'APOL': 'APPLE}
Any ideas to fix it? What I’m doing wrong?
@Luke: The text file is in https://easyupload.io/x6v37a
– Ed S
Ok. I checked here. That’s right. I’ll edit my reply to include the removal of ' t'
– Lucas
@Lucas: could you please explain in more detail?
– Ed S