python文件读取每一行操作

https://blog.csdn.net/wsLJQian/article/details/81210867
终于解决了多年以来为什么读取txt文件时总是从第二行开始打印的问题,记住readline是读一行,readlines才是读所有行
第一行:

import cv2


f = open("result_final_2.txt","r")   #设置文件对象
lines = f.readlines()
count = 0
a = []
b = []

import numpy as np
img1 = cv2.imread('l.png',0)

img2 = cv2.imread('r.png',0)


j = 0
jj = -1


    
for line in lines:  #直到读取完文件
    

    
    count = count + 1
    l = line.split('\t')[0]
    if count != 1213:

        jj = jj + 1
        
        a.append(l)                    #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
        a.append(img1[1, j])
        a.append(img2[1, jj])
        a.append(img1[1, j]-img2[1, jj])
        print(a)
        a = []
        b.append(a) 
    else:
        count = 1
        j = j + 1
        jj = 0
        a.append(l)                    #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
        a.append(img1[1, j])
        a.append(img2[1, jj])
        a.append(img1[1, j]-img2[1, jj])
        print(a)
        a = []
        b.append(a) 

np.savetxt("b.txt", b,fmt='%s',delimiter=','

第三到10行:

import cv2


f = open("result_final_3.txt","r")   #设置文件对象
lines = f.readlines()
count = 0
a = []
b = []

import numpy as np
img1 = cv2.imread('l.png',0)

img2 = cv2.imread('r.png',0)


j = 0
jj = -1

i = 2
cnt = 0 
    
for line in lines:  #直到读取完文件
    cnt = cnt + 1
    if cnt > 1468944:
        cnt = 1
        i = i + 1
        count = 0
        j = 0
        jj = -1


    count = count + 1
    l = line.split('\t')[0]
    if count != 1213:

        jj = jj + 1
        
        a.append(l)                    #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
        a.append(img1[i, j])
        a.append(img2[i, jj])
        a.append(int(img1[i, j])-int(img2[i, jj]))
        print(a)
        a = []
        b.append(a) 
    else:
        count = 1
        j = j + 1
        jj = 0
        a.append(l)                    #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
        a.append(img1[i, j])
        a.append(img2[i, jj])
        a.append(int(img1[i, j])-int(img2[i, jj]))
        print(a)
        a = []
        b.append(a) 
    
np.savetxt("c.txt", b,fmt='%s',delimiter=',')

第11到20行:

import cv2


f = open("result_final_4.txt","r")   #设置文件对象
lines = f.readlines()
count = 0
a = []
b = []

import numpy as np
img1 = cv2.imread('l.png',0)

img2 = cv2.imread('r.png',0)


j = 0
jj = -1

i = 10
cnt = 0 
    
for line in lines:  #直到读取完文件
    cnt = cnt + 1
    if cnt > 1468944:
        cnt = 1
        i = i + 1
        count = 0
        j = 0
        jj = -1


    count = count + 1
    l = line.split('\t')[0]
    if count != 1213:

        jj = jj + 1
        
        a.append(l)                    #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
        a.append(img1[i, j])
        a.append(img2[i, jj])
        a.append(int(img1[i, j])-int(img2[i, jj]))
        print(a)
        a = []
        b.append(a) 
    else:
        count = 1
        j = j + 1
        jj = 0
        a.append(l)                    #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
        a.append(img1[i, j])
        a.append(img2[i, jj])
        a.append(int(img1[i, j])-int(img2[i, jj]))
        print(a)
        a = []
        b.append(a) 
    


np.savetxt("d.txt", b,fmt='%s',delimiter=',')

21-30

import cv2


f = open("result_final_5.txt","r")   #设置文件对象
lines = f.readlines()
count = 0
a = []
b = []

import numpy as np
img1 = cv2.imread('l.png',0)

img2 = cv2.imread('r.png',0)


j = 0
jj = -1

i = 20
cnt = 0 
    
for line in lines:  #直到读取完文件
    cnt = cnt + 1
    if cnt > 1468944:
        cnt = 1
        i = i + 1
        count = 0
        j = 0
        jj = -1


    count = count + 1
    l = line.split('\t')[0]
    if count != 1213:

        jj = jj + 1
        
        a.append(l)                    #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
        a.append(img1[i, j])
        a.append(img2[i, jj])
        a.append(int(img1[i, j])-int(img2[i, jj]))
        print(a)
        a = []
        b.append(a) 
    else:
        count = 1
        j = j + 1
        jj = 0
        a.append(l)                    #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
        a.append(img1[i, j])
        a.append(img2[i, jj])
        a.append(int(img1[i, j])-int(img2[i, jj]))
        print(a)
        a = []
        b.append(a) 
    
np.savetxt("e.txt", b,fmt='%s',delimiter=',')

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值