本帖最后由 codegay 于 2016-10-1 01:52 编辑
python3
楼主这个数据源太不多科学了。应该切分成列合并,统一存到一个数据库或者表格中。需要的时候再查询出来。
尼马地这样各种分片拼接字符串,又搞各种类型转换,麻烦累,效率还低。- #2016年10月1日 00:25:33 codegay
-
- with open("截取号码段.txt") as f:
- 号码段 = [r.strip().split("-") for r in f.readlines()]
- result = []
- for s,e in 号码段:
- fn = s[:4] #filename
- start = s[4:11]
- end = e[4:11]
- numlist = [fn+str(r).rjust(7,'0') for r in range(int(start),int(end)+1)]
- with open(fn+".txt") as f:
- txt = f.readlines()
- for n in numlist:
- result += [l for l in txt if l.startswith(n)]
-
- print(result)
-
- with open("newresult.txt","w+") as f:
- f.writelines(result)
复制代码
|