python – 从文件中读取时剥离空格和新行
发布时间:2020-12-20 12:34:19 所属栏目:Python 来源:网络整理
导读:我有以下代码,在从文件读取时成功地删除行尾字符,但是对于任何前导和尾随空格都没有这样做(我希望中间的空格被留下!) 实现这一目标的最佳方法是什么? (注意,这是一个具体的例子,因此不能删除剥离字符串的一般方法) 我的代码:(尝试使用测试数据:“Moose先
我有以下代码,在从文件读取时成功地删除行尾字符,但是对于任何前导和尾随空格都没有这样做(我希望中间的空格被留下!)
实现这一目标的最佳方法是什么? (注意,这是一个具体的例子,因此不能删除剥离字符串的一般方法) 我的代码:(尝试使用测试数据:“Moose先生”(未找到),如果你尝试“Moose先生”(这是Moose之后的空间),它将起作用. #A COMMON ERROR is leaving in blank spaces and then finding you cannot work with the data in the way you want! """Try the following program with the input: Mr Moose ...it doesn't work.......... but if you try "Mr Moose " (that is a space after Moose..."),it will work! So how to remove both new lines AND leading and trailing spaces when reading from a file into a list. Note,the middle spaces between words must remain? """ alldata=[] col_num=0 teacher_names=[] delimiter=":" with open("teacherbook.txt") as f: for line in f.readlines(): alldata.append((line.strip())) print(alldata) print() print() for x in alldata: teacher_names.append(x.split(delimiter)[col_num]) teacher=input("Enter teacher you are looking for:") if teacher in teacher_names: print("found") else: print("No") 生成列表alldata时所需的输出 ['Mr Moose:Maths','Mr Goose:History','Mrs Congenelipilling:English'] 即 – 删除开头处以及分隔符之前或之后的所有前导和尾随空格.必须留下像穆斯先生这样的词之间的空间. 教师内容: Mr Moose : Maths Mr Goose: History Mrs Congenelipilling: English 提前致谢 解决方法
你可以使用正则表达式:
txt=''' Mr Moose : Maths Mr Goose: History Mrs Congenelipilling: English''' >>> [re.sub(r's*:s*',':',line).strip() for line in txt.splitlines()] ['Mr Moose:Maths','Mrs Congenelipilling:English'] 所以你的代码变成: import re col_num=0 teacher_names=[] delimiter=":" with open("teacherbook.txt") as f: alldata=[re.sub(r's*{}s*'.format(delimiter),delimiter,line).rstrip() for line in f] print(alldata) for x in alldata: teacher_names.append(x.split(delimiter)[col_num]) print(teacher_names) 打印: ['Mr Moose:Maths','Mrs Congenelipilling:English'] ['Mr Moose','Mr Goose','Mrs Congenelipilling'] 关键部分是正则表达式: re.sub(r's*{}s*'.format(delimiter),line).rstrip() ^ 0 to unlimited spaced before the delimiter ^ place for the delimiter ^ unlimited trailing space Interactive Demo 对于所有Python解决方案,我将使用str.partition获取分隔符的左侧和右侧,然后根据需要删除空白: alldata=[] with open("teacherbook.txt") as f: for line in f: lh,sep,rh=line.rstrip().partition(delimiter) alldata.append(lh.rstrip() + sep + rh.lstrip()) 相同的输出 另一个建议.您的数据更适合dict而不是列表. 你可以做: di={} with open("teacherbook.txt") as f: for line in f: lh,rh=line.rstrip().partition(delimiter) di[lh.rstrip()]=rh.lstrip() 或理解版本: with open("teacherbook.txt") as f: di={lh.rstrip():rh.lstrip() for lh,_,rh in (line.rstrip().partition(delimiter) for line in f)} 然后像这样访问: >>> di['Mr Moose'] 'Maths' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |