我正在试着写一篇文章(下载.txt)来自打开.txt不存在相同的“id”,并且在异常中没有(idexception,classexception)。我已经写了“身份证”不重复了。在
我的问题是如何添加条件'classexception',我试过了,但这是不可能的。你知道我要用的词典/条件句吗?在
c = open('open.txt','r') #structure: name:xxx; id:xxxx; class:xxxx; name:xxx; id:xxxx;class:xxxx etc
t=c.read()
d=open('download.txt','a')
allLines = t.split("\n")
lines = {}
class=[s[10:-1] for s in t.split() if s.startswith("class")]
for line in allLines:
idPos = line.find("id:")
colPos = line.find(";",idPos)
if idPos > -1:
id = line[idPos+4: colPos if colPos > -1 else None]
if id not in idexception:
lines.setdefault(id,line)
for l in lines:
d.write(lines[l]+'\n')
c.close()
d.close()
一般来说,你很不清楚,但如果我理解正确,我的方法是处理你的问题,里面有很多评论:
相关问题 更多 >
编程相关推荐