您的位置:首页 > Web前端 > BootStrap

Django 上传文件(bootstrap file input插件) 中文乱码 解决 (python3)

2017-11-08 12:19 1806 查看
上传文件示例:



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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐