python问题读取写入文件问题
答案:1 悬赏:0 手机版
解决时间 2021-04-01 22:17
- 提问者网友:谁的错
- 2021-04-01 14:17
python问题读取写入文件问题
最佳答案
- 五星知识达人网友:逐風
- 2021-04-01 15:53
def SaveToTxt(fileB, fileC,lsContents):
nLine=0
try:
fOutB = open(fileB + ".txt","w",encoding="utf-8")
fOutC = open(fileC + ".txt","w",encoding="utf-8")
for item in lsContents:
nLine+=1 # index begin with 1 rather than 0
if nLine %2 ==0
fOutC.write("%d: %s
"% (nLine,item))
else
fOutB.write("%d: %s
"% (nLine,item))
except Exception as e:
print('!!!save file %s error ---%s' % (file,e))
finally:
fOutC.close()
fOutB.close()
追问文件A呢??追答大哥,文件a你按行读一下不会写么?然后把读出来的结果传给lsContents。
def LoadFile(filepath):
lsContents=[]
try:
fp = open(filepath,'r',encoding='utf-8')
for line in fp:
lsContents.append(line)
except Exception as e:
print("... load file failed %s" % (e))
finally:
fp.close()
return lsContents追问不会写。能写的简单一点吗?不要套函数。。直接就A B C,最好用WITH OPEN,不会OPEN也行,反正这问题就你一个解答。。分也是你的,送佛送到西把追答大哥我回答你的问题是为了帮你解决问题,分不分的无所谓。好吧,我整理一下发给你。
nLine=0
try:
fOutB = open(fileB + ".txt","w",encoding="utf-8")
fOutC = open(fileC + ".txt","w",encoding="utf-8")
for item in lsContents:
nLine+=1 # index begin with 1 rather than 0
if nLine %2 ==0
fOutC.write("%d: %s
"% (nLine,item))
else
fOutB.write("%d: %s
"% (nLine,item))
except Exception as e:
print('!!!save file %s error ---%s' % (file,e))
finally:
fOutC.close()
fOutB.close()
追问文件A呢??追答大哥,文件a你按行读一下不会写么?然后把读出来的结果传给lsContents。
def LoadFile(filepath):
lsContents=[]
try:
fp = open(filepath,'r',encoding='utf-8')
for line in fp:
lsContents.append(line)
except Exception as e:
print("... load file failed %s" % (e))
finally:
fp.close()
return lsContents追问不会写。能写的简单一点吗?不要套函数。。直接就A B C,最好用WITH OPEN,不会OPEN也行,反正这问题就你一个解答。。分也是你的,送佛送到西把追答大哥我回答你的问题是为了帮你解决问题,分不分的无所谓。好吧,我整理一下发给你。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯