推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wsds
V2EX  ›  Python

Python 写了一个读取 2 个文件,比较第一行的脚本,横看竖看不对劲,没看出来什么错误,

  •  
  •   wsds · Aug 10, 2018 · 5931 views
    This topic created in 2839 days ago, the information mentioned may be changed or developed.

    使用场景: 以其中一个文件为基准(正确的值),对比两个文件中不同的行,并统计出来正确的数,错误的数 如

    1.txt (基准文件)
    a.jpg 0
    b.jpg 2
    c.jpg 3
    ...
    
    2.txt
    a.jpg 1
    b.jpg 2
    c.jpg 3
    ...
    

    那么这两个文件中就 a.jpg 出错了 我的思路比较粗爆,就是拿 1 这个基准的每一行去了空格后,与 2 里边比,比中就记个数,一直把 1 中的挨个与 2 中的比一遍,但我发现这样写,每次 都要重新打开文件 2 一次,而且感觉不也靠谱,而且不知道怎么把错误的给挑出来,有什么更好的办法吗?或者是优化一下我这个

    def compare(testfile, basefile):
        correct = 0
        all = 0
        with open(testfile, encoding="utf-8") as f1:
            for i in f1.readlines():
                all += 1
                with open(basefile, encoding="utf-8") as f2:
                    for j in f2.readlines():
                        if i.replace(" ", "") == j.replace(" ", ""):
                            correct += 1
                        else:#想把错误的输出出来,但没有输出
                            print(i)#想把错误的输出出来
        print("样本数: %d\n 正确数: %d\n 错误数: %d\n 准确率: %f" %
              (all, correct, all - correct, correct / all))
    
    
    if __name__ == '__main__':
        compare("test1.txt", "base1.txt")
    
    15 replies    2018-08-10 21:53:04 +08:00
    Yourshell
        1
    Yourshell  
       Aug 10, 2018 via iPhone
    在循环里别用 with 呗,直接读文件读完关掉
    ivechan
        2
    ivechan  
       Aug 10, 2018
    没必要非得用 with。
    fffflyfish
        3
    fffflyfish  
       Aug 10, 2018
    读到 numpy 或者 pandas,然后直接比对列值,很简单呀
    0xLeco
        4
    0xLeco  
       Aug 10, 2018   ❤️ 1
    return len(set(line.strip() for line in open(testfile)) - set(line.strip() for line in open(basefile)))
    imn1
        5
    imn1  
       Aug 10, 2018
    你实际上是同行数比较吧?
    这样只需要一个 for 行数 就够了

    你现在是两文件每行都交叉比较,数量级是相乘
    awah
        6
    awah  
       Aug 10, 2018
    数据量不大可以直接 set 了,再取交集吧
    wsds
        7
    wsds  
    OP
       Aug 10, 2018
    @imn1 不是同行,是要交叉,可能 我举的例子看上去每一行都一样,其实不是这样的,是乱序的
    wsds
        8
    wsds  
    OP
       Aug 10, 2018
    @kethylar 这个太牛逼了,能不能把这个错误的值也显示出来啊
    wsds
        9
    wsds  
    OP
       Aug 10, 2018
    @kethylar 知道了,把 len 去掉就显示了,多谢
    elsove812
        10
    elsove812  
       Aug 10, 2018 via iPhone
    分别打开前一个个文件,用前面的“ a.jpg ”、“ b.jpg ”....当作 key 后面的当作值存入字典,再打开第二个文件,逐行读入,用前一段去字典里取值,与后面的对比。
    0xLeco
        11
    0xLeco  
       Aug 10, 2018
    @wsds 小文件可以这样搞,大点的文件借助一下 redis 啥的(或者直接比较 hash 值)
    wsds
        12
    wsds  
    OP
       Aug 10, 2018 via iPhone
    @kethylar 小文件指的是多小?
    0xLeco
        13
    0xLeco  
       Aug 10, 2018 via Android
    @wsds 内存不爆我觉得都小。。。
    wsds
        14
    wsds  
    OP
       Aug 10, 2018
    @kethylar 那应该没啥压力,哈哈
    thedrwu
        15
    thedrwu  
       Aug 10, 2018 via Android
    sort | comm -3 | wc -l
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3911 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 00:55 · PVG 08:55 · LAX 17:55 · JFK 20:55
    ♥ Do have faith in what you're doing.