Skip to main content
 首页 » 编程设计

python之在 Python 中获取文本文件的换行统计信息

2025年05月04日58sxdcgaq8080

我在 git 文件中有一个令人讨厌的 CRLF/LF 冲突,这可能是从 Windows 机器上提交的。是否有跨平台的方式(最好是在 Python 中)来检测哪种类型的换行符在文件中占主导地位?

我有这段代码(基于 https://stackoverflow.com/a/10562258/239247 的想法):

import sys 
if not sys.argv[1:]: 
  sys.exit('usage: %s <filename>' % sys.argv[0]) 
 
with open(sys.argv[1],"rb") as f: 
  d = f.read() 
  crlf, lfcr = d.count('\r\n'), d.count('\n\r') 
  cr, lf = d.count('\r'), d.count('\n') 
  print('crlf: %s' % crlf) 
  print('lfcr: %s' % lfcr) 
  print('cr: %s' % cr) 
  print('lf: %s' % lf) 
  print('\ncr-crlf-lfcr: %s' % (cr - crlf - lfcr)) 
  print('lf-crlf-lfcr: %s' % (lf - crlf - lfcr)) 
  print('\ntotal (lf+cr-2*crlf-2*lfcr): %s\n' % (lf + cr - 2*crlf - 2*lfcr)) 

但它给出了错误的统计信息(对于 this file ):

crlf: 1123 
lfcr: 58 
cr: 1123 
lf: 1123 
 
cr-crlf-lfcr: -58 
lf-crlf-lfcr: -58 
 
total (lf+cr-2*crlf-2*lfcr): -116 

请您参考如下方法:

import sys 
 
 
def calculate_line_endings(path): 
    # order matters! 
    endings = [ 
        b'\r\n', 
        b'\n\r', 
        b'\n', 
        b'\r', 
    ] 
    counts = dict.fromkeys(endings, 0) 
 
    with open(path, 'rb') as fp: 
        for line in fp: 
            for x in endings: 
                if line.endswith(x): 
                    counts[x] += 1 
                    break 
    print(counts) 
 
 
if __name__ == '__main__': 
    if len(sys.argv) == 2: 
        calculate_line_endings(sys.argv[1]) 
 
    sys.exit('usage: %s <filepath>' % sys.argv[0]) 

为您的文件提供输出

crlf: 1123 
lfcr: 0 
cr: 0 
lf: 0 

够了吗?