rmcknst2
rmcknst2

Reputation: 307

Removing end whitespace form strings in a list

I have a list of emails when printed look like this (they look like this because of how the csv file is set up. I can't change that)

enter image description here

The code I have is:

list1=[x.strip().split(',')for x in list1]

but its giving me an error: 'list' object has no attribute 'strip'

I have also tried:

list1[filt_count]=list1[filt_count].Trim()

but it gives an error: 'list' object has no attribute 'Trim'

Expected Outcome: Now obviously these are example emails and the list is gonna be much larger (over 500 emails all said and done) enter image description here

Upvotes: 0

Views: 49

Answers (2)

CyanDeathReaper
CyanDeathReaper

Reputation: 43

You have a list of lists and you are not really using csv to it's extent. What you want doesn't require you to use x.strip().split(",") so it is unnecessary to have. The correct answer would be [[x.strip() for x in y] for y in list1]

Upvotes: 0

paul
paul

Reputation: 468

First: please avoid posting images. It's much easier to reproduce what you do when we can cut and paste your code.

1) What you have is a list of lists. This needs to be take account of in the comprehension. 2) x.strip().split(',') does not make sense, as you are not dealing with a comma separated string

[[x.strip() for x in l] for l in list1]

Upvotes: 3

Related Questions