[英]How to write words from list to file?
“ test.txt”中有兩個sendnecs
句子1 =句子是由一個或多個單詞組成的語法單位。
句子2 =句子也可以單獨用拼字法定義。
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”中的第一個句子中寫入單詞並跟在其后?
有什么建議嗎?
在您的代碼中,您多次打開和關閉輸出文件,導致代碼覆蓋您從第一句話開始編寫的內容。 簡單的解決方案是只打開一次,然后關閉一次。
count_line = 0
# Open outside the loop
file = open('C:/Users/Desktop/test_words.txt', 'w+')
for line in open('C:/Users/Desktop/test.txt'):
count_line = count_line +1
fields = line.rstrip('\n').split('\t')
##print count_line, fields
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')
# Also close outside the loop
file.close()
發生這種情況的原因是,當您第二次打開文件時,沒有保留其中的原始文本。 當您打開文件並用Python對其進行寫入時,除非將其存儲在變量中並重新編寫,否則基本上會覆蓋其內容。
試試這個代碼:
count_line = 0
for n, line in enumerate(open('test.txt')):
count_line = count_line +1
fields = line.rstrip('\n').split('\t')
##print count_line, fields
already_text = open('test_words.txt').read() if n > 0 else ''
file = open('test_words.txt', 'w+')
count_word = 0
file.write(already_text)
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()
這是我運行它時的輸出:
1 A 2 sentence 3 is 4 a 5 grammatical 6 unit 7 consisting 8 of 9 one 10 or 11 more 12 words. 1 A 2 sentence 3 can 4 also 5 be 6 defined 7 in 8 orthographic 9 terms 10 alone.
這是沒有enumerate()
的代碼:
count_line = 0
n = 0
for line in open('test.txt'):
count_line = count_line +1
fields = line.rstrip('\n').split('\t')
##print count_line, fields
already_text = open('test_words.txt').read() if n > 0 else ''
file = open('test_words.txt', 'w+')
count_word = 0
file.write(already_text)
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()
n += 1
如果可能的話,你應該使用with
與文件打交道時-這是一個上下文管理器,並確保它們是正確關閉,一旦你與他們進行(這是由離開縮進塊指示)。 在這里,我們使用enumerate
並提供了可選的start
參數-這是一種(少數幾種)使計數器在移至下一行時保持運行的方式:
# Open the file
with open('test.txt', 'rb') as f:
# Open the output (in Python 2.7+, this can be done on the same line)
with open('text_words.txt', 'wb') as o:
# Set our counter
counter = 1
# Iterate through the file
for line in f:
# Strip out newlines and split on whitespace
words = line.strip().split()
# Start our enumeration, which will return the index (starting at 1) and
# the word itself
for index, word in enumerate(words, counter):
# Write the word to the file
o.write('{0} {1}\n'.format(index, word))
# Increment the counter
counter += len(words)
或者,如果您希望減少行數,則可以使用readlines()
將文件讀入列表,列表中的行用換行符分隔。 然后,將行本身在空白處分割,並拔出每個單詞。 這意味着您基本上可以遍歷文件中所有單詞的列表,並且與enumerate
結合使用就不需要為您完成計數器的增加:
# Open the file
with open('test.txt', 'rb') as f:
# Open the output (in Python 2.7+, this can be done on the same line)
with open('text_words.txt', 'wb') as o:
# Iterate through the file
for i, w in enumerate((x for l in f.readlines() for x in l.strip().split()), 1):
o.write('{0} {1}\n'.format(i, w))
使用Python 2.7:
# Open the file
with open('test.txt', 'rb') as f, open('text_words.txt', 'wb') as o:
# Iterate through the file
for i, w in enumerate((x for l in f.readlines() for x in l.strip().split()), 1):
o.write('{0} {1}\n'.format(i, w))
這可能無關緊要,但我建議您使用更簡潔的方法來編寫。 您不需要3個循環:
lines = open('test.txt').readlines()
file = open('test_words.txt', 'w+')
for line in lines:
words = line.rstrip('\n').split()
for i, word in enumerate(words):
print i, word
file.write('%d %s\n' % (i+1, word))
file.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.