2012-05-05 21 views
0

在test.txt中,我有兩行句子。如何在第一行顯示句子(多行)的行號?

The heart was made to be broken. 
There is no surprise more magical than the surprise of being loved. 

在代碼:

import re 
file = open('test.txt','r')#specify file to open 
data = file.readlines() 
file.close() 

print "---------------------------------------------------" 
count = 0 
for line in data: 
    line_split = re.findall(r'[^ \t\n\r, ]+',line) 
    count = count + 1 
    def chunks(line_split, n): 
     for i in xrange(0, len(line_split), n): 
      yield line_split[i:i+n] 

    separate_word = list(chunks(line_split, 8)) 

    for i, word in enumerate(separate_word, 1): 
     print count, ' '.join(word) 
    print "---------------------------------------------------" 

結果從代碼:

--------------------------------------------------- 
1 The heart was made to be broken. 
--------------------------------------------------- 
2 There is no surprise more magical than the 
2 surprise of being loved. 
--------------------------------------------------- 

是否有僅在第一行顯示句的數量任何可能的方式?

期望的結果:

--------------------------------------------------- 
1 The heart was made to be broken. 
--------------------------------------------------- 
2 There is no surprise more magical than the 
    surprise of being loved. 
--------------------------------------------------- 
+1

別t將標題添加到語言名稱 - 這是標籤的用途。 –

回答

1

簡單地檢查它是否是第一行:

for i, word in enumerate(separate_word): 
    if i == 0: 
     print count, ' '.join(word) 
    else: 
     print " ", ' '.join(word) 

我強烈建議你使用the with statement打開該文件。這是更具可讀性,並處理關閉文件,即使在例外。

另一個好主意是直接在文件上循環 - 這是一個更好的主意,因爲它不會立即將整個文件加載到內存中,這是不需要的,並可能導致大文件出現問題。

你也應該使用enumerate(),因爲你已經在這裏做了data循環,因爲這樣你不會手動處理count

您也重複定義chunks(),這有點無意義,最好在開始時定義一次。在調用它的地方,也不需要列表 - 我們可以直接在生成器上迭代。

如果我們糾正了這一切,我們得到的清潔:

import re 

def chunks(line_split, n): 
    for i in xrange(0, len(line_split), n): 
     yield line_split[i:i+n] 

print "---------------------------------------------------" 

with open("test.txt", "r") as file: 
    for count, line in enumerate(file, 1): 
     line_split = re.findall(r'[^ \t\n\r, ]+',line) 
     separate_word = chunks(line_split, 8) 
     for i, word in enumerate(separate_word): 
      if i == 0: 
       print count, ' '.join(word) 
      else: 
       print " ", ' '.join(word) 

     print "---------------------------------------------------" 

另外值得一提的變量名是有點誤導word,例如,是不發一語。

+0

i == 0不顯示任何內容。這是正確的,如果我= 1 – ThanaDaray

+0

@ThanaDaray請注意我從'enumerate(separate_word,1)'改爲'enumerate(separate_word)'。 –

+0

@sarnold呃,固定。 –

0

Python帶有內置的我承認,下面的格式是不完美的文本換行,但你會得到的想法:-)

#!/usr/bin/env python 

import sys 
import textwrap 

with open('test.txt') as fd: 
    T = [line.strip() for line in fd] 

for n, s in enumerate(T): 
    print '-'*42 
    sys.stdout.write("%d " % n) 
    for i in textwrap.wrap(s, 45): 
     sys.stdout.write("%s\n" % i) 
print '-'*42 

輸出:

------------------------------------------ 
0 The heart was made to be broken. 
------------------------------------------ 
1 There is no surprise more magical than the 
surprise of being loved. 
------------------------------------------