nonen
nonen

Reputation: 29

how to use strip() in python

I have a text file test.txt which has in it 'a 2hello 3fox 2hen 1dog'.
I want to read the file and then add all the items into a list, then strip the integers so it will result in the list looking like this 'a hello fox hen dog'

I tried this but my code is not working. The result is ['a 2hello 3foz 2hen 1dog']. thanks

newList = [] 
filename = input("Enter a file to read: ") 
openfile = open(filename,'r')

for word in openfile:
    newList.append(word)



for item in newList:
    item.strip("1")
    item.strip("2")
    item.strip("3")

print(newList)
openfile.close()

Upvotes: 2

Views: 9828

Answers (5)

Atom
Atom

Reputation: 40

Another way to use it is:

l=[]
for x in range(5):  
   l.append("something")
l.strip()

This will remove all spaces

Upvotes: 0

Praveen
Praveen

Reputation: 9365

from python Doc

str.strip([chars])
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:

Strip wont modify the string, returns a copy of the string after removing the characters mentioned.

>>> text = '132abcd13232111'
>>> text.strip('123')
'abcd'
>>> text
'132abcd13232111'

You can try:

out_put = []
for item in newList:
    out_put.append(item.strip("123"))

If you want to remove all 123 then use regular expression re.sub

import re
newList = [re.sub('[123]', '', word) for word in openfile]

Note: This will remove all 123 from the each line

Upvotes: 4

glglgl
glglgl

Reputation: 91159

A function in Python is called in this way:

result = function(arguments...)

This calls function with the arguments and stores the result in result.

If you discard the function call result as you do in your case, it will be lost.

Upvotes: 2

tzaman
tzaman

Reputation: 47870

Pointers:

  • strip returns a new string, so you need to assign that to something. (better yet, just use a list comprehension)
  • Iterating over a file object gives you lines, not words;
  • so instead you can read the whole thing then split on spaces.
  • The with statement saves you from having to call close manually.
  • strip accepts multiple characters, so you don't need to call it three times.

Code:

filename = input("Enter a file to read: ") 
with open(filename, 'r') as openfile:
    new_list = [word.strip('123') for word in openfile.read().split()]
print(new_list)

This will give you a list that looks like ['a', 'hello', 'fox', 'hen', 'dog']
If you want to turn it back into a string, you can use ' '.join(new_list)

Upvotes: 3

midori
midori

Reputation: 4837

there are several types of strips in python, basically they strip some specified char in every line. In your case you could use lstrip or just strip:

s = 'a 2hello 3fox 2hen 1dog'
' '.join([word.strip('0123456789') for word in s.split()])

Output:

'a hello fox hen dog'

Upvotes: 2

Related Questions