file.write() 保留 creating/returning 一个空文件
file.write() keeps creating/returning an empty file
我正在尝试写入文件,但代码无法正常工作,并且 write()
一直返回空文本文件。
with open('UFOGB_Observations.txt', 'r') as UFO_Obsr:
lines_txt = UFO_Obsr.readlines()
print('file read')
#Tell python where a column ends:
for x in range(0, ):
UFO_Obsr.split('\t')
with open ('UFO_Observations_1.txt', 'w+') as UFO_Obsw:
##with a for loop, iterating through the rows to
##change all instances of html codes(I found a few other html codes!):
for line in UFO_Obsr:
line = line.replace(''', "'")
line = line.replace('!', '!')
line = line.replace('&', '&')
UFO_Obsw.write(line)
print('File written')
您应该遍历 lines_txt
,您已经通过执行 readlines()
或删除 lines_txt = UFO_Obsr.readlines()
到达文件末尾
#Tell python where a column ends:
for x in range(0, ):
UFO_Obsr.split('\t')
没有做任何事情,所以我将其省略。
原文件每一行,修改后写入新文件
with open('UFOGB_Observations.txt', 'r') as UFO_Obsr, open ('UFO_Observations_1.txt', 'w+') as UFO_Obsw:
##with a for loop, iterating through the rows to
##change all instances of html codes(I found a few other html codes!):
for line in UFO_Obsr:
line = line.replace(''', "'")
line = line.replace('!', '!')
line = line.replace('&', '&')
UFO_Obsw.write(line)
print('File written')
我正在尝试写入文件,但代码无法正常工作,并且 write()
一直返回空文本文件。
with open('UFOGB_Observations.txt', 'r') as UFO_Obsr:
lines_txt = UFO_Obsr.readlines()
print('file read')
#Tell python where a column ends:
for x in range(0, ):
UFO_Obsr.split('\t')
with open ('UFO_Observations_1.txt', 'w+') as UFO_Obsw:
##with a for loop, iterating through the rows to
##change all instances of html codes(I found a few other html codes!):
for line in UFO_Obsr:
line = line.replace(''', "'")
line = line.replace('!', '!')
line = line.replace('&', '&')
UFO_Obsw.write(line)
print('File written')
您应该遍历 lines_txt
,您已经通过执行 readlines()
或删除 lines_txt = UFO_Obsr.readlines()
#Tell python where a column ends:
for x in range(0, ):
UFO_Obsr.split('\t')
没有做任何事情,所以我将其省略。
原文件每一行,修改后写入新文件
with open('UFOGB_Observations.txt', 'r') as UFO_Obsr, open ('UFO_Observations_1.txt', 'w+') as UFO_Obsw:
##with a for loop, iterating through the rows to
##change all instances of html codes(I found a few other html codes!):
for line in UFO_Obsr:
line = line.replace(''', "'")
line = line.replace('!', '!')
line = line.replace('&', '&')
UFO_Obsw.write(line)
print('File written')