0

在 test.txt 中,我有 2 行句子。

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.
---------------------------------------------------
4

2 回答 2

1

只需检查它是否是第一行:

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

我强烈建议您使用with语句打开文件。这更具可读性,并为您处理关闭文件,即使在异常情况下也是如此。

另一个好主意是直接遍历文件 - 这是一个更好的主意,因为它不会一次将整个文件加载到内存中,这不是必需的,并且可能会导致大文件出现问题。

你也应该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,例如,不是一个词。

于 2012-05-05T22:10:05.753 回答
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.
------------------------------------------
于 2012-05-05T22:44:40.013 回答