如何从文本文件中删除此分号(;)。我的文本文件的内容:
201911007,1,28;
201203008,1,28;
199710014,1,28;
201612010,1,28;
201710017,1,28;
然后python读取它,如果我运行代码,它总是在28的末尾有分号
with open("empMR.txt", 'r') as files:
for dln in files:
dln = dln.strip()
if len(dln) >= 1:
lii = dln.split(",")
MR_empno.append(lii[0].strip())
month.append(lii[1].strip())
days_work.append(lii[2].strip())
print(days_work)
输出:['28;'、'28;'、'28;'、'28;'、'28;'、'28;']我想删除每个输出上的分号(;) 预期产出[28,28,28,28,28]
只需包含要删除的字符(包括;)
您不需要向其他函数添加额外的调用,尤其是不需要正则表达式(使用正则表达式可能还有其他正当理由,请参见下面的示例)
有几种方法可以解决这个问题
.replace()
方法,类似dln.replace(';', '')
的方法可能会有所帮助dln = dln[:-1]
可能会有帮助您选择哪种方法取决于您对数据看起来总是一样的信心,以及您对哪种方法更满意
正则表达式是处理字符串的强大工具
相关问题 更多 >
编程相关推荐