上傳文件示例:
python3代碼:
f = request.FILES.get('playerList', None) # 獲取 上傳文件,如果沒有,默認爲 None
''' 寫入文件 bytes方式 '''
fileName = 'levelRewards_' + str( int( time.time()*1000 - 8*60*60*1000 ) ) + '.txt' # UTC時間戳,ms,int取整
path = 'slg/uploads/' + fileName
with open(path, 'w') as destination: # 寫方式
for chunk in f.chunks():
destination.write( chunk.decode() ) # python3 中,直接 將 bytes 轉換爲 utf-8 就行,直接 decode(),沒有encode了!!!!!!!
destination.close()
''' 讀取文件 '''
playerList = [] # 形如: [ [10000121321, 1000230, 20], [10002133222, 1000222, 10;] ]
with open(path, 'r', encoding='utf-8') as f: # 只讀方式
for line in f:
line = line.strip() # 去掉 換行符 \n
line = line.replace(";", "") # 去掉 最後一位的 “ ; ”號
playerList.append( line.split(', ') )
# print(playerList)
f.close()