|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
with open('D:\\setupact.log','r') as f1:
with open('D:\\setupact_new.log','w') as f2:
m=0
i=1
while True:
a=f1.readline()
if a!='':
if len(a)>m:
m=len(a)
else:
f1.seek(0,0)
break
print(m)
while True:
b=f1.readline()
if b=='\n':
f2.write(' '*(m+5-len(b))+'#'+str(i)+'\n')
i+=1
else:
b=b.rstrip()
f2.write(b+' '*(m+5-len(b))+'#'+str(i)+'\n')
i+=1
if b=='':
break
以上是我自己写的代码为什么运行之后出现空行的行号对不齐,跪求大佬解答
- with open(r'C:\Users\lenovo\Desktop\temp\python\USB.log','r', encoding='utf-8') as f1:
- with open(r'C:\Users\lenovo\Desktop\temp\python\USB_new.log','w+', encoding='utf-8') as f2:
- m=0
- i=1
- for a in f1:
- max_length = round((len(a.encode('utf-8')) - len(a))/2 + len(a)) ##此处参考网上计算中文长度的算法
- if max_length>m:
- m=max_length
- f1.seek(0,0)
- for b in f1:
- if b=='\n':
- f2.write(' '*(m+5)+'#'+str(i)+'\n')
- i+=1
- else:
- b=b.rstrip()
- length = int((len(b.encode('utf-8')) - len(b))/2 + len(b)) #计算当前行宽度
- f2.write(b+' '*(m+5-length)+'#'+str(i)+'\n')
- i+=1
复制代码
网上搜了一个计算中文字符宽度的算法,但是还有bug:
1.“·”这个符号如果当前行包含超过1个,则计算不准
2.日志文件中不能存在tab符号
3.其他还有什么字符不支持尚不清楚
|
|