【问题标题】:Reading corrupted file in python在python中读取损坏的文件
【发布时间】:2010-01-23 18:40:57
【问题描述】:

我有一个文件,看起来像这样alt text http://img40.imageshack.us/img40/4581/crapq.png 现在显示了 5 行。但是运行这个脚本

with open('hello.txt', 'r') as hello:
    for line in hello:
        print line,

给予

num 1
ctl00$header1$Login1$txtUserName=ыют;CBШ▌

仅此而已。如何读取整个文件? TIA

【问题讨论】:

    标签: python


    【解决方案1】:
    entire_file = open('hello.txt', 'rb').read()
    
    print 'number of \\n: %d, number of bytes %d' % (
        entire_file.count('\n'), len(entire_file))
    

    【讨论】:

    • @roddik: 是的,将 'b' 标志添加到 open() 以表明您希望将文件作为二进制文件(例如字节序列)处理。
    猜你喜欢
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 2015-09-24
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    相关资源
    最近更新 更多