“test.txt”中有两个sentnec
sentence1 = 句子是由一个或多个单词组成的语法单元。
sentence2 = 一个句子也可以单独用正字法定义。
count_line = 0
for line in open('C:/Users/Desktop/test.txt'):
count_line = count_line +1
fields = line.rstrip('\n').split('\t')
##print count_line, fields
file = open('C:/Users/Desktop/test_words.txt', 'w+')
count_word = 0
for words in fields:
wordsplit = words.split()
for word in wordsplit:
count_word = count_word + 1
print count_word, word
file.write(str(count_word) + " " + word + '\n')
file.close()
我在“test_words.txt”中的结果只显示了第二句中的单词:
1 A
2 sentence
3 can
4 also
5 be
6 defined
7 in
8 orthographic
9 terms
10 alone.
如何也将第一句中的单词写在第二句“test_words.txt”中的单词之后?
有什么建议吗?