Python 如何在第一行显示句子的行号(多行)?

Python 如何在第一行显示句子的行号(多行)?,python,Python,在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 "--------------------

在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.
---------------------------------------------------
代码的结果:

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

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

for i, word in enumerate(separate_word):
    if i == 0:
        print count, ' '.join(word)
    else:
        print " ", ' '.join(word)
我强烈建议您使用打开该文件。这更具可读性,可以为您处理关闭文件的问题,即使是在异常情况下

另一个好主意是直接在文件上循环-这是一个更好的主意,因为它不会立即将整个文件加载到内存中,这是不需要的,并且可能会导致大文件出现问题

您还应该像这里一样使用
数据
,这样您就不会手动处理
计数

您还重复定义
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不是word。

Python内置了文本包装。我承认下面的格式并不完美,但你会明白的:-)

产出:

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

不要在标题中添加语言名称-这就是标记的作用。@ThanaDaray注意我从
enumerate(separate_-word,1)
enumerate(separate_-word)
@ThanaDaray的更改没有问题。如果它能解决你的问题,请放心。--我知道。上面说我必须等8分钟。@ThanaDaray当然,如果你没有,就告诉你。@ThanaDaray什么不是,对不起?