Python - Remove elements from a list

Asked

Viewed 282 times

0

I have a list in Python and I want to replace certain elements in it, already try to use the replace mode I found in the documentation, but I did not succeed because it cannot be used in lists.

I have a list as follows.

Mylist = ['Ford"~"Fiat"~"Ka"~"Palio']

This list is composed of a single element, using the function Len(line) it returns me the value 1.

I need to treat this line to get out the following way.

NewList= ['Ford','Fiat','Ka','Palio']

I’m trying in many ways and I’m not getting the expected result.

My code:

encoding='utf-8'

import csv

NewMatriz = []


ficheiro = open('C:\\Users\\asus\\Documents\\models.csv', 'r', newline='', encoding="utf8")
reader = csv.reader(ficheiro)


for item in reader:
    NewMatriz.append(item.replace('"','').split('~'))
  • What is the content of models.csv?

2 answers

1


One way to do this is to remove double quotes with the function replace and turn the string into an array with the function split on the basis of (~):

#Lista simples
Mylist = ['Ford"~"Fiat"~"Ka"~"Palio']

NewList = Mylist[0].replace('"','').split('~')

print(NewList)

#Lista matriz

#Com loop
NewMatriz = []

for item in Mylist:
  NewMatriz.append(item.replace('"','').split('~'))

print(NewMatriz)

#Com map/lambda
NewMapList = []

NewMapList = map(lambda x: x.replace('"','').split('~'), Mylist)
print(list(NewMapList))

In the other examples, I created one within another, just to illustrate that there may be other forms, but with slightly different results, but it can be useful in its resolution.


Working with the CSV:

encoding='utf-8'

import csv

NewMatriz = []

ficheiro = open('C:\\Users\\asus\\Documents\\models.csv', 'r', newline='\n', encoding="utf8")
reader = csv.reader(ficheiro)

for line in reader:
  for item in line:
    NewMatriz.append(item.replace('"','').split('~'))

print(NewMatriz)
  • This way worked, however when I take this data from a csv, it returns me the following message 'Attributeerror: 'list' Object has no attribute 'replace''

  • I added the code in the example

  • Put your CSV in, too, please

  • Added, are data I took on the site of Anac, has more than 5000 lines, I’m trying to treat this data

  • The reading of the CSV is different, I put another example, I hope it helps you.

  • It worked!! Thank you very much, I was trying and I wasn’t getting it.

  • Great, if possible then please accept the answer.

  • Done, just had not done, because did not know this option. Thank you!!!

Show 3 more comments

0

Searches the csv.Reader documentation for any arguments you can use to reset the field separator. I use the pandas library and the read_csv function, which I find much more friendly. In it, you can define the argument "Sep" that identifies in the reading of the file which character is being used to separate the fields. In your example, to be the "~". Another possibility is that the encoding is not right. Maybe it is reading "~" when it is actually a ";". Try encoding "latin" or "latin1" and see if the output is different.

  • This does not answer the question. When you have reputation enough, you’ll be able to leave comments on any post but until then, write only answer that no depend on more information of who asked. - Of Revision

  • 2

    Actually, Eduardo touches on an excellent point. If the CSV separator is correctly configured AP will not need to do this gambit to replace the strings. The problem is that the answer is not very descriptive, but you can work on it.

  • Thanks for the tip Eduardo, I will research on this yes

  • 1

    @Andréfilipe, thank you for the guidance. Starting now to actually participate collaborating. I will look into this in the next. abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.