解析固定寬度檔案(每列在一行中佔據特定數量的字元)可能是一項需要效率的任務。以下是關於如何有效實現這一目標的討論:
考慮一個固定寬度的文件,其中前20 個字元代表一列,後面的21-30 代表第二列,依此類推在。給定一行 100 個字符,我們如何有效地將其解析為各自的列?
1.結構模組:
利用 Python 標準函式庫的 struct 模組由於其 C 實作而提供了簡單性和速度。下面的程式碼示範了其用法:
<code class="python">import struct fieldwidths = (2, -10, 24) fmtstring = ' '.join('{}{}'.format(abs(fw), 'x' if fw <p>輸出:</p> <pre class="brush:php;toolbar:false">fmtstring: '2s 10x 24s', record size: 36 chars fields: ('AB', 'MNOPQRSTUVWXYZ0123456789')
2.最佳化的字串切片:
雖然字元串切片很常用,但對於大行來說它可能會變得很麻煩。這是一種最佳化方法:
<code class="python">from itertools import zip_longest from itertools import accumulate def make_parser(fieldwidths): # Calculate slice boundaries. cuts = tuple(cut for cut in accumulate(abs(fw) for fw in fieldwidths)) # Create field slice tuples. flds = tuple(zip_longest(cuts, (0,)+cuts))[:-1] # Ignore final value. # Construct the parsing function. parse = lambda line: tuple(line[i:j] for i, j in flds) parse.size = sum(abs(fw) for fw in fieldwidths) parse.fmtstring = ' '.join('{}{}'.format(abs(fw), 'x' if fw <p>輸出:</p> <pre class="brush:php;toolbar:false">fmtstring: '2s 10x 24s', record size: 36 chars fields: ('AB', 'MNOPQRSTUVWXYZ0123456789')
以上是如何在Python中有效解析固定寬度的檔案行?的詳細內容。更多資訊請關注PHP中文網其他相關文章!