string - Python unicode errors while writing to file -
i'm using python 2.7 parse through bunch of webpages , content them, webpages include characters "" , ', both somehow converted ’. gives me file content looks (excluding quotes): "i think it’s important..."
the strings print out fine in terminal using print()
method, can't seem same effect using print >> file, string
or file.write(string)
. encoding issue, i've searched no success find way around this. i'm opening file this: file = codecs.open("file.txt","w+", encoding='utf-8')
, i'm using beautifulsoup4's gettext()
method assign strings values. there way solve this?
try add below lines code in start of function, solve problem.
import sys reload(sys) sys.setdefaultencoding('utf8')
Comments
Post a Comment