Coding problems are often encountered when coding with Python, Unicodeencodeerror: ' ASCII ' codec can ' t encode characters in position 0-1: Ordinal not in range (128) , in fact, coding problems can be solved by decode and encode.
The representation of a string inside Python is Unicode encoding, so in encoding conversion, it is usually necessary to use Unicode as the intermediate encoding, that is, decoding the other encoded string (decode) into Unicode first. From Unicode encoding (encode) to another encoding.
The role of Decode is to convert other encoded strings into Unicode encodings, such as Str1.decode (' gb2312 '), to convert gb2312 encoded string str1 into Unicode encoding.
The role of encode is to convert Unicode encoding into other encoded strings, such as Str2.encode (' gb2312 '), to convert Unicode encoded string str2 to gb2312 encoding.
Practical application:
For I in range (len (Hot_weibo)):
File_tianchong= "\n********************\n"
File_.write (Hot_weibo[i].encode (' Utf-8 ')) #将unicode编码转换成utf8
File_.write (Read_num[i].encode (' Utf-8 '))
File_.write (File_tianchong)
Python Coding issues