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()