How to process huge text files that contain EOF / Ctrl-Z characters using Python on Windows?

前端 未结 1 1600
悲&欢浪女
悲&欢浪女 2020-12-03 16:05

I have a number of large comma-delimited text files (the biggest is about 15GB) that I need to process using a Python script. The problem is that the files sporadically con

相关标签:
1条回答
  • 2020-12-03 16:15

    It's easy to use Python to delete the DOS EOF chars; for example,

    def delete_eof(fin, fout):
        BUFSIZE = 2**15
        EOFCHAR = chr(26)
        data = fin.read(BUFSIZE)
        while data:
            fout.write(data.translate(None, EOFCHAR))
            data = fin.read(BUFSIZE)
    
    import sys
    ipath = sys.argv[1]
    opath = ipath + ".new"
    with open(ipath, "rb") as fin, open(opath, "wb") as fout:
        delete_eof(fin, fout)
    

    That takes a file path as its first argument, and copies the file but without chr(26) bytes to the same file path with .new appended. Fiddle to taste.

    By the way, are you sure that DOS EOF characters are your only problem? It's hard to conceive of a sane way in which they could end up in files intended to be treated as text files.

    0 讨论(0)
提交回复
热议问题