简体   繁体   English

如何在python中使用strip()

[英]how to use strip() in python

I have a text file test.txt which has in it 'a 2hello 3fox 2hen 1dog' . 我有一个文本文件test.txt ,里面有'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' 我想读取文件,然后将所有项目添加到列表中,然后strip整数,这样会导致列表看起来像这样的'a hello fox hen dog'

I tried this but my code is not working. 我尝试了这个,但我的代码不起作用。 The result is ['a 2hello 3foz 2hen 1dog'] . 结果是['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()

from python Doc 来自python Doc

str.strip([chars]) str.strip([字符])
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. chars参数是一个字符串,指定要删除的字符集。 If omitted or None, the chars argument defaults to removing whitespace. 如果省略或None,则chars参数默认为删除空格。 The chars argument is not a prefix or suffix; chars参数不是前缀或后缀; rather, all combinations of its values are stripped: 相反,它的所有值组合都被剥离:

Strip wont modify the string, returns a copy of the string after removing the characters mentioned. Strip不会修改字符串,在删除提到的字符后返回字符串的副本。

>>> 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 如果要删除所有123使用正则表达式re.sub

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

Note: This will remove all 123 from the each line 注意:这将从每行中删除所有123

Pointers: 指针:

  • strip returns a new string, so you need to assign that to something. strip返回一个字符串,因此您需要将其指定给某个字符串。 (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. 所以相反,你可以read整个事物,然后split空格。
  • The with statement saves you from having to call close manually. with语句使您无需手动调用close
  • strip accepts multiple characters, so you don't need to call it three times. strip接受多个字符,因此您不需要调用它三次。

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'] 这会给你一个看起来像['a', 'hello', 'fox', 'hen', 'dog']
If you want to turn it back into a string, you can use ' '.join(new_list) 如果要将其重新转换为字符串,可以使用' '.join(new_list)

there are several types of strips in python, basically they strip some specified char in every line. python中有几种类型的条带,基本上它们在每一行中都删除了一些指定的char。 In your case you could use lstrip or just strip : 在你的情况下你可以使用lstrip或只是strip

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

Output: 输出:

'a hello fox hen dog'

A function in Python is called in this way: Python中的函数以这种方式调用:

result = function(arguments...)

This calls function with the arguments and stores the result in result . 这就需要function与参数和结果存储到result

If you discard the function call result as you do in your case, it will be lost. 如果您丢弃函数调用结果,就像丢弃函数调用结果一样,它将丢失。

Another way to use it is: 另一种使用方法是:

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

This will remove all spaces 这将删除所有空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM