1. 程式人生 > >TensorFlow學習筆記7——Win7下Spyder執行python程式碼出現UnicodeEncodeError問題的解決

TensorFlow學習筆記7——Win7下Spyder執行python程式碼出現UnicodeEncodeError問題的解決

環境:

        win7+Anaconda1.6.3+Spyder3.2.1+tensorflow1.1.0

問題描述:

        執行下述程式碼,讀取之前儲存的tfrecord檔案,發生錯誤。

import tensorflow as tf

files = tf.train.match_filenames_once("./output/training-images/*.tfrecords")
#雖然沒有variable,但也必須init,並且global和local都要有,不然會報錯,不信你試試。
init = [tf.global_variables_initializer(), tf.local_variables_initializer()]
with tf.Session() as sess:
    sess.run(init)
    print (sess.run(files))

錯誤提示為UnicodeEncodeError: 'utf-8' codec can't encode character '\udcd5' in position 2255: surrogates not allowed。

解決:

     問題發生在tf.train.match_filenames_once這句話。網上提問這個錯誤的很多,但沒有好的解決辦法。也浪費了我大半天的時間,如程式所示,我的路徑不含中文啊。想到可能是路徑問題,我便把專案的整個資料夾拷貝到桌面上,沒出現錯誤!返回到我E盤的專案中,我看到E盤根目錄下經過幾級目錄後,才到專案的路徑,即:E:\★11-python\tensorflow\例程\StanfordDog_test\output\training-images,把這裡的中文也改成英文E:\★11-python\tensorflow\example\StanfordDog_test\output\training-images

,成功了!

總結:

    出現上述錯誤時,要把從硬碟開始一直到檔案最後的目錄都改成英文,而不是隻改子目錄。如果還有問題,請檢查並確保計算機名稱中也不含中文字元。

然而!

     我重啟電腦,把之前改成英文的路徑再改回E:\★11-python\tensorflow\例程\StanfordDog_test\output\training-images,程式執行成功了,沒報錯!!!!這個真不懂咋回事了。o(╥﹏╥)o