繁体   English   中英

从字符串列表中去除不需要的字符

[英]Stripping out unwanted characters from a list of strings

我有一个名为file_contents的字符串列表。 列表中的每个项目都以数字开头,格式为:#1。 #2。 等等。我想从列表中的每个项目中删除这些内容。

for item in range(len(file_contents)):
    file_contents[item].lstrip('#' + [item] + ". ")

因此,我想将"#1. Apples"转换为"Apples"

有什么建议么?

运行此命令时,出现以下错误:

TypeError: Can't convert 'list' object to str implicitly

这是我正在定义的整个方法:

def read_from_file(self, filename):
        """Checks if file exists, if it does, reads it in and creates new List object."""
        file_contents = []
        fileExists = os.path.isfile(filename)
        if not fileExists:
            print(filename, "does not exist.")
        else:
            with open(filename) as file:
                file_contents = [line.strip() for line in file]

        for item in range(len(file_contents)):
            file_contents[item] = file_contents[item].lstrip('#' + str(item) + ". ")

        list_name = file_contents[0]
        list_contents = []
        for item in file_contents:
            if item in list_name:
                continue
            else:
                list_contents.append(item)

        new_list = List(list_name)
        new_list.contents = list_contents

        return new_list

正则表达式非常适合:

import re
pattern = re.compile(r'#\d+\.\s*')
new_contents = [pattern.sub('', item) for item in file_contents]

我建议阅读doc链接以查看regex的工作原理,但对模式进行简要说明:

  • # -寻找#字符
  • \\d+ -后跟一位或多位数字
  • \\. -然后是一个点字符
  • \\s* -任意数量的空格

re.sub查找该模式,然后将其替换为空字符串'' -从而将其切掉。

您还极大地误解了lstrip和Python语法的一般工作原理:

  1. 它不会修改您调用的字符串,而是返回一个新字符串。
  2. [item]只会是[0][1]等,这就是为什么您不能将其连接到字符串的原因。 我不太确定您要达到的目标。

我想你的意思是

stripped_contents = []
with open('test.data') as f:
    for i, line in enumerate(f):
        strip = '#' + str(i + 1) + ". "
        stripped_line = line.lstrip(strip)
        stripped_contents.append(stripped_line)

print stripped_contents

即您需要将项目转换为字符串而不是列表。 另外,因为它从0开始,所以您需要项+ 1。

另一个解决方案可能是

stripped_contents = []
with open('test.data') as f:
    for i, line in enumerate(f):
        start_pos = len('#' + str(i + 1) + ". ")
        stripped_line = line[start_pos:]
        stripped_contents.append(stripped_line)

print stripped_contents

正则表达式也将起作用。 但是对于这样一个简单的问题来说,感觉太复杂了。

如果您不想从左侧剥离,请将所有字符传递给lstrip:

def read_from_file(self, filename):
        """Checks if file exists, if it does, reads it in and creates new List object."""
        file_contents = []
        fileExists = os.path.isfile(filename)
        if not fileExists:
            return (filename, "does not exist.")
        with open(filename) as file:
            file_contents = [line.lstrip("0123456789.").strip() for line in file]

您要删除换行符,因此只需调用strip即可,之后将删除换行符和开头的空格:

In [14]: "#123. 1foo".lstrip("0123456789#.").strip()
Out[14]: '1foo'

暂无
暂无

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

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