Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

587
Visualizações
How do we remove ' ' from the list?

So I am trying to read line by line from the text file using python And I got list of lists. And I have to use the elements of the list to create dictionary. So what I did was

list1 = []
for line in file:
   lines = line.strip().split()
   list1.append(lines)
print(list1)

And what I got when I ran it is the list of lists but something different than what I wanted to get, I got something like this,

['a,b,c,d,e,f']

What I wanted to get was something like this,

[a,b,c,d,e,f]

So how do we get rid of that ' ' inside the list? I tried to use remove method, but it did not work.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

you just need to change the separator in the strip from the default white space to ",". i.e you just need to add the separator argument in the strip function .

list1 = []
for line in file:
   lines = line.strip().split(',')
   list1.append(lines)
print(list1)

so if text is:

a,b,c,d,e,f

the strip function would take the separator as "," and create a list for it.

hence the output would be:

['a','b','c','d','e','f']

therefore finally you just need to change the separator argument of line.strip() from white spaces to comma separated list and you are done with it .

over 4 years ago · Santiago Trujillo Relatório

0

What you have here is a list with a single element. It contains a single string which is 'a,b,c,d,e,f'.

We can double-check:

data = ['a,b,c,d,e,f']
print(len(data)) # prints 1
print(data[0])   # prints 'a,b,c,d,e,f'

What you want is to split this string into a list:

row = data[0].split(',')

Now row contains what you expected data to be, which is ['a', 'b', 'c', 'd', 'e', 'f'].

In a real case you would probably fix this while reading the file, the code might look like this:

list1 = []

with open('test.csv') as test_file:
    for line in test_file:
        list1.append(line.strip().split(','))

print(list1)

Now you would then have a list per line in your input file:

[
    ['a', 'b', 'c', 'd', 'e', 'f'], 
    ['g', 'h', 'i', 'j', 'k', 'l'],
]
over 4 years ago · Santiago Trujillo Relatório

0

Just change the delimeter from '' to ','

list1 = []
for line in file:
   lines = line.strip().split(,)
   list1.append(lines)
print(list1)

you are good to go

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda