python去除文件中数据前的空格,去除文件中的空行

with open (r'E:/tadb.out') as f:
    for line in f.readlines():
        line=line.lstrip()
        print (line)
        with open('E:/1.out','a') as f2:
            f2.write(line)
mq1=open('E:/mqtt.out','r')
mq2=open('E:/2.out','w')
try:
    for line in mq1.readlines():
        if line == '\n':
            line=line.strip("\n")
        mq2.write(line)
finally:
    mq1.close()
    mq2.close()
# with open (r'E:/tadb.out') as f:
#     for line in f.readlines():
#         line=line.lstrip()
#         print (line)
#         with open('E:/1.out','a') as f2:
#             f2.write(line)


# mq1=open('E:/mqtt.out','r')
# mq2=open('E:/2.out','w')
# try:
#     for line in mq1.readlines():
#         if line == '\n':
#             line=line.strip("\n")
#         mq2.write(line)
# finally:
#     mq1.close()
#     mq2.close()

f=open('E:/1.out','r')
f1=open('E:/2.out','r')
f2=open('E:/3.out','w')
result=[]

for line in f.readlines():
    a=line.split(":")[0]
    a=a.strip("\"")
    result.append(a)
    # print (result)

# for res in result:
    # print(res)
num = 0
for line in f1.readlines():
    # print(line)
    b=line.split("\\\"timestamp\\\":")[1]
    c=b.split(",")[0]
    if c not in result:
        f2.write(line)
        num += 1
print(num)
f.close()
f1.close()

 

你可能感兴趣的:(python)