String.strip()重要性

时间:2015-11-13 03:33:44

标签: python string file strip

我的任务是编写一个函数,将文件中单词的长度与整数进行比较,然后返回所有大小的单词。我得到的答案几乎相同,只是我没有像他们那样包含string.strip():

def get_words(dictfile,size): words = []
  for word in open(dictfile).readlines():
      word = word.strip() #this is the only bit I didn't have in my own code
      if len(word) == size:
          words.append(word) 
  return words

离开.strip()会不会真正改变这个函数的输出,还是在处理文件时把它放进来是个好习惯?

编辑: 输入将是一个文件,其中每个单词都是一行,例如

a
abandon
abbey
abdominal

,大小只是任何整数

3 个答案:

答案 0 :(得分:3)

def get_number_of_words(filename, size):
    words = []
    with open(filename) as dictfile:
        for line in dictfile:
            word = line.strip()
            if len(word) == size:
                words.append(word)
    return words

我用更多的东西改写了你的功能" call-a-spade-a-spade"变量名称以清除正在发生的事情。让我们谈谈我取代的一些事情:

  • for word in open(dictfile).readlines():

通过匿名方式打开文件,您抛弃了对用于关闭文件对象的文件对象的引用。此外,您不必要使用readlines将整个文件读入内存。相反,我更喜欢with上下文管理器,它允许您保存对文件句柄的引用(使用as),并在完成后自动关闭文件,这是您忽略的。 (从技术上讲,Python最终会为你做这件事,但它仍然是一个好习惯。)

另请注意,我直接在文件for line in dictfile上进行迭代 - 这比将整个文件前加载到内存中要有效得多。

至于您的标题问题,如果您希望此功能提供准确的计数,则必须在此处致电line.strip()。您迭代的文件中的每一行都包含一个'\n'字符(换行符),由len计算,因为它是字符串的一部分。如你所知,如果你问函数文件中有多少长度为4的单词,它会给你所有长度为3的单词(第4个字符为'\n',人类通常不会这样做计数)。

答案 1 :(得分:1)

根据您的输入,它可能会产生影响。这意味着最好将它放在那里。

鉴于您每行读一个单词,strip()存在以删除前导或尾随空格。 E.G。:

word1
  word2
word3   

word2将显示比没有strip()的其他人更长的时间。这也适用于之后的空白,通过查看输入文件也很难发现(我甚至找不到在这个答案中直观地表示它的好方法)

编辑: 正如@ Two-Bit Alchemist在评论中指出的那样,\n字符也需要被剥离,否则你会有一个1分之一的错误。这个字符用作行结尾,因此人们通常不会注意到它,但Python解释器会将其考虑在内。

答案 2 :(得分:1)

在@Knells答案之上添加更多点数,      String.Strip()函数不仅用于在没有作为strip的参数提供时删除尾随空格,它可以从结尾处开始删除您指定为参数的任何字符或字符列表例如

的字符串
str1 = " Saltharion   "
//will result in "Saltharion"
str1.Strip() 

str2 = "ooHow are yuoo"
// Will result in "How are yu", here the char "o" at the `end and beginning` is stripped from string
str2.strip("o") 

str3 = "ar How are you, I am good are" 
// will result in "How are you, I am good", here it will not strip occurrence of "are" it will strip occurrence of "a" "r" "e" from end and beginning
str3.strip("are")

请查看文档here