from unidecode import *
reader = open("a.txt",'w')
def unite():
for line in reader:
line = unidecode(line)
print (line)
unite()
現在,我收到一條錯誤消息,指出在寫入模式下不允許循環。有沒有其他的方式,我可以修改每一行,像這樣,它可以使用unidecode轉換?用於文本文件中「for循環」的替代方法
from unidecode import *
reader = open("a.txt",'w')
def unite():
for line in reader:
line = unidecode(line)
print (line)
unite()
現在,我收到一條錯誤消息,指出在寫入模式下不允許循環。有沒有其他的方式,我可以修改每一行,像這樣,它可以使用unidecode轉換?用於文本文件中「for循環」的替代方法
你可以把它全部放在記憶裏。
from unidecode import *
reader = open("a.txt",'w')
lines = reader.readlines()
def unite(lines):
for line in lines:
line = unidecode(line)
print (line)
unite()
您也可以使用臨時文件。
from unidecode import *
import os
reader = open('a.txt','r')
temp = open('~a.txt', 'w')
for line in reader():
line = unidecode(line)
temp.write(line)
reader.close()
temp.close()
os.remove('a.txt')
os.rename('~a.txt', 'a.txt')
'對於閱讀器中的行是足夠的 - 取決於python版本'readlines()'將把整個文件存儲在內存中。這對於小文件來說很好,但有可能耗盡大內存的所有內存。 –
@PauloScardine謝謝我改變了代碼來反映這一點。 – Drew
您的工作最好,謝謝:D – madprogramer
您可以追加方式打開它:
def unite():
with open('somefile.txt','a+') as f:
for line in f:
f.write(unidecode(line))
print line
unite()
這寫東西到文件的末尾。要從文件開頭寫入內容,請使用r+
模式。
例如:
sample.txt
:
hello world
當你運行這個:
with open('sample.txt','a+') as f:
line = f.readline()
f.write('{} + again'.format(line.strip()))
文件將具有:
hello world
hello world again
如果你運行:
with open('sample.txt','r+') as f:
line = f.readline()
f.write('{} + once more'.format(line.strip()))
文件將具有:
hello world
hello world once more
hello world again
如果你想替換文件的的內容,那麼你就可以讀取該文件,保存線,將其關閉並在寫打開模式寫回線條。
這是一個有點骯髒的祕密,但很少的應用程序能夠真正改變一個文件「到位」。大多數情況下,它看起來應用程序正在修改文件,但在引擎蓋下,編輯後的文件被寫入臨時位置,然後移動以替換原始文件。
如果你想一想,當你在文件的中間插入幾個字節時,無論如何你必須從這一點重寫整個文件。
由於ASCII輸出往往較小,則Unicode輸入,你大概可以拉斷是這樣的(僅適用於UNIX,我猜):
#!/usr/bin/env python
import os
from unidecode import unidecode
def unidecode_file(filename):
# open for write without truncating
fd = os.open(filename, os.O_WRONLY)
pos = 0 # keep track of file length
# open for read
with open(filename) as input:
for line in input:
ascii = unidecode(line.decode('utf-8'))
pos += len(ascii)
os.write(fd, ascii)
os.ftruncate(fd, pos) # truncate output
os.close(fd) # that is all, folks
if __name__ == '__main__':
unidecode_file('somefile.txt')
這個噱頭是不是安全,不規範編輯文件的方式(如果輸出大於輸入,您肯定會遇到麻煩)。使用Drew建議的臨時文件方法,但要確保文件名唯一性(最安全的方法是爲臨時文件生成一個隨機文件名)。
閱讀所有的行,修改,然後寫回來。 AFAIK你不能像這樣修改線條。 – elssar
如何在閱讀時編輯文件? – madprogramer
@madprogramer你不能同時讀寫。考慮打開另一個文件進行編輯。 – squiguy