2011-02-03 12 views
4

大きな文字列のテキストファイルがあります。117文字ごとに文字列を分割し、改行に次の117文字を入れたいと思います。 "私は、視認性の理由から文字列を削除した """ スペース= "" "Python、文字列を分割する

""" ファイル=オープン( 'testoutput.txtを' `S = "":

私はこれを試してみました)、 'W' 一方の: プリントS [10] 出力=出力+ S + ""」

""" 
s = s[10:] 

file.write(出力) file.close() プリント "" 完了 `

が、ファイル内の最終的な出力は、このカスケードように見えた問題だった: `この[SHIFT] R [Backspace]キー分子とその子孫は、[Backspace]キー、[Backspace]キーための突然変異

T]r[BACKSPACE]molecule and its descendants would av[BACKSPACE][BACKSPACE]vary because of mutations 



ACE]molecule and its descendants would av[BACKSPACE][BACKSPACE]vary because of mutations 



le and its descendants would av[BACKSPACE][BACKSPACE]vary because of mutations 



descendants would av[BACKSPACE][BACKSPACE]vary because of mutations 



ts would av[BACKSPACE][BACKSPACE]vary because of mutations 



v[BACKSPACE][BACKSPACE]vary because of mutations 



E][BACKSPACE]vary because of mutations 



CE]vary because of mutations 



cause of mutations 



utations 
の異なるAVでしょうが

`

答えて

6
while s: 
    print s[:117] 
    s = s[117:] 
+0

damnnn私にそれを打つ – tekknolagi

3

次のいずれかの定期的なスライス構文を使用してバッファを分割することができ、またはそれを読みながら、あなたは直接ファイルを分割することを選ぶことがあります。これは2番目のアプローチの例です:

with open(r"/path/to/some/file") as input_file: 
    line = input_file.read(117) 
    while line: 
     print len(line) 
     line = input_file.read(117)