備忘錄_20160105(定位) 修改 回首頁

程式 2022-07-11 15:02:40 1657522960 100
python 讀寫文字檔案(跨編碼)

python 讀寫文字檔案(跨編碼)

from os import walk

strMyPath='0500-0599';

oaF=[]
for (oaDirpath, oaDirnames, oaFilenames) in walk(strMyPath):
  oaF.extend(oaFilenames)
  break

oFileWrite=open(strMyPath+'.txt', 'w', encoding='utf8')

for iIndex in range(len(oaF)):
  
  iIdx4Num=oaF[iIndex].find("_")
  str期數='未知'
  if iIdx4Num!=-1:
    str期數=oaF[iIndex][0:iIdx4Num]
  str期數='【明倫月刊'+str期數+'期】'
  
  oFileRead=open(strMyPath+'/'+oaF[iIndex], 'r', encoding='utf_16_le')
  strContent=oFileRead.read()
  oFileRead.close()
  
  oFileWrite.write("\r\n"+str期數+"\r\n")
  oFileWrite.write(strContent)
  
  print(strMyPath+'/'+oaF[iIndex], str期數)
  # print(strContent)

oFileWrite.close()

print("finished")