python基操3_读取excel并写入txt(判断无重复写入)

import openpyxl
wb = openpyxl.load_workbook('pin_list.xlsx')
sheet = wb.get_sheet_by_name('Sheet2')

for i in range(2,25,1):
    if sheet.cell(row=i,column=2).value == 'O':
        data = 'input   '+sheet.cell(row=i,column=1).value +'\n'
        print(data)
        with open("test1.txt","a",encoding="utf-8") as f:
            line1 = open(r"test1.txt").readlines()
            print(line1)
            j = 0
            for x in line1:#判断data1是否在列表中
                print(x)
                if x.strip() == data.strip()://去掉双方的换行符
                    print(1)
                    j = j + 1
                else:
                    j = j
            print(j)
            if j == 0:
                f.write(data)
            else:
                print('repetition')

for i in range(2,25,1):
    if sheet.cell(row=i,column=2).value == 'I/O':
        data1 = 'input   '+sheet.cell(row=i,column=1).value +'_oe\n'
        data2 = 'input   '+sheet.cell(row=i,column=1).value +'_out\n'
        print(data1)
        with open("test1.txt","a",encoding="utf-8") as f:
            line2 = open(r"test1.txt").readlines()
            print(line2)
            j = 0
            for x in line2:#判断data1是否在列表中
                print(x)
                if x.strip() == data1.strip():
                    print(1)
                    j = j + 1
                else:
                    j = j
            print(j)
            if j == 0:
                f.write(data1)
                f.write(data2)
            else:
                print('repetition')

for i in range(2,25,1):
    if sheet.cell(row=i,column=2).value == 'I':
        data = 'output   '+sheet.cell(row=i,column=1).value +'\n'
        print(data)
        with open("test1.txt","a",encoding="utf-8") as f:
            line3 = open(r"test1.txt").readlines()
            print(line3)
            j = 0
            for x in line3:#判断data1是否在列表中
                print(x)
                if x.strip() == data.strip():
                    print(1)
                    j = j + 1
                else:
                    j = j
            print(j)
            if j == 0:
                f.write(data)
            else:
                print('repetition')
for i in range(2,25,1):
    if sheet.cell(row=i,column=2).value == 'I/O':
        data = 'output   '+sheet.cell(row=i,column=1).value +'_in\n'
        print(data)
        with open("test1.txt","a",encoding="utf-8") as f:
            line4 = open(r"test1.txt").readlines()
            print(line4)
            j = 0
            for x in line4:#判断data1是否在列表中
                print(x)
                if x.strip() == data.strip():
                    print(1)
                    j = j + 1
                else:
                    j = j
            print(j)
            if j == 0:
                f.write(data)
            else:
                print('repetition')

在这里插入图片描述
结果:
input a6
input a8
input b1
input b2
input b3
input b8
input a4_oe
input a4_out
input a5_oe
input a5_out
input b4_oe
input b4_out
input b5_oe
input b5_out
input b6_oe
input b6_out
input b7_oe
input b7_out
output a1
output a2
output a3
output a7
output a9
output a10
output b9
output b10
output a4_in
output a5_in
output b4_in
output b5_in
output b6_in
output b7_in

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值