(python)两文件行数内容对比,输出二者不存在的内容
本文最后更新于 1406 天前,其中的信息可能已经有所发展或是发生改变。

代码部分:

#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
"""
两文件行数内容对比,输出二者不存在的内容
"""
import datetime
import os


def check_file(file1, file2):
    time = str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))

    file1_name = os.path.split(file1)[1]
    file2_name = os.path.split(file2)[1]

    str1 = []
    str2 = []
    str_dump = []
    f1 = open(file1, 'r', encoding='UTF-8')
    f2 = open(file2, 'r', encoding='UTF-8')

    for line in f1.readlines():
        str1.append(line.replace("\n", ''))

    for line in f2.readlines():
        str2.append(line.replace("\n", ''))
    f1.close()
    f2.close()

    for i in str1:
        if i not in str2:
            i = file1_name+','+i
            str_dump.append(i)

    for i in str2:
        if i not in str1:
            i = file2_name+','+i
            str_dump.append(i)

    # f3 = open('target_{}.txt'.format(time), 'w+')
    # for i in list(str_dump):
    #     f3.write(i + '\n')
    # f3.close()

    with open('target_{}.txt'.format(time), 'w+') as f3:
        for i in list(str_dump):
            f3.write(i + '\n')


if __name__ == '__main__':
    check_file("1.txt", "2.txt")

 

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇