未验证 提交 0fd67bac 编写于 作者: M mapingshuo 提交者: GitHub

Merge pull request #1532 from PaddlePaddle/mapingshuo-patch-1

modify open file code for python3
...@@ -21,7 +21,12 @@ import numpy as np ...@@ -21,7 +21,12 @@ import numpy as np
import time, datetime import time, datetime
import os, sys import os, sys
def maybe_open(filepath):
if sys.version_info <= (3, 0): # for python2
return open(filepath, 'r')
else:
return open(filepath, 'r', encoding="utf-8")
def Glove840B_300D(filepath, keys=None): def Glove840B_300D(filepath, keys=None):
""" """
input: the "glove.840B.300d.txt" file path input: the "glove.840B.300d.txt" file path
...@@ -33,7 +38,7 @@ def Glove840B_300D(filepath, keys=None): ...@@ -33,7 +38,7 @@ def Glove840B_300D(filepath, keys=None):
print("please wait for a minute.") print("please wait for a minute.")
start = time.time() start = time.time()
word2vec = {} word2vec = {}
with open(filepath, "r") as f: with maybe_open(filepath) as f:
for line in f: for line in f:
if sys.version_info <= (3, 0): # for python2 if sys.version_info <= (3, 0): # for python2
line = line.decode('utf-8') line = line.decode('utf-8')
......
...@@ -68,8 +68,10 @@ def maybe_open(file_name): ...@@ -68,8 +68,10 @@ def maybe_open(file_name):
" |- readme.txt\n" " |- readme.txt\n"
" |- wordvec.txt\n") " |- wordvec.txt\n")
raise RuntimeError(msg) raise RuntimeError(msg)
if sys.version_info <= (3, 0): # for python2
return open(file_name, 'r') return open(file_name, 'r')
else:
return open(file_name, 'r', encoding="utf-8")
def tokenized_question_pairs(file_name): def tokenized_question_pairs(file_name):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册