0
I did this function to extract the desired column from csv. Then, make an append in column[] passing a while to only take the Dice from the corresponding column. And finally a condition to separate str from int.
My doubt is in the return of this data, that instead of bringing only the type of data str it also returns the int. Thank you in advance.
%%writefile carros.csv
id│valor_venda│valor_manutencao│portas│pessoas│porta_malas
1 │vhigh │med │2 │2 │small
2 │med │vhigh │2 │2 │small
3 │low │vhigh │2 │2 │small
4 │low │high │2 │2 │small
5 │low │high │2 │2 │small
6 │low │high │4 │4 │big
7 │low │high │4 │4 │big
8 │low │med │2 │2 │small
9 │low │med │2 │2 │small
10│low │med │2 │2 │small
11│low │med │4 │4 │big
12│low │low │2 │2 │small
13│low │low │4 │4 │small
14│low │low │4 │4 │med
def extrai_coluna_csv(nome_arquivo: str, indice_coluna: int, tipo_dado: str):
coluna = []
## leia o arquivo com o comando 'with' utilizando o parametro 'nome_arquivo'
with open(file=nome_arquivo, mode='r', encoding='utf8') as fp:
## extraia a coluna do arquivo utilizando o parametro 'indice_coluna'
linha = fp.readline()
linha_separada = linha.split(sep=',')
linha = linha_separada[indice_coluna]
coluna.append(linha)
## use a estrutura de decisão if/elif/else para fazer a conversão do tipo de dados utilizando o parametro 'tipo_dado'
while linha:
if tipo_dado == 'str':
coluna.append(linha)
elif tipo_dado == 'int':
coluna.append(linha)
else:
print('Error')
linha = fp.readline()
return coluna
## extrair a coluna valor_venda
valor_venda = extrai_coluna_csv(
nome_arquivo='./carros.csv', indice_coluna=1, tipo_dado='str')
print(valor_venda) # deve retornar ['vhigh', 'med', 'low', ...]
yes, first post here on stackoverflow, already edited. I just couldn’t pass the csv file. Thanks for your attention.
– Asher
I understand what the program does, but what the doubt is?
– Danizavtz
My return cannot only take the values of # ['vhigh', 'med', 'low', ...], it returns all lines and also the values of int, when only the datatype must be str.
– Asher