Section 3: My computer hates me.

Almost 20+ days rolled by and still there are something I am so afraid to dead with it.

As zoom-quiet(大妈)said: "在 win 环境中编程的都是上好几辈子被反复折翼的天使..."

At the beginning, I spent so much time to install and configure the programming environment of python/powershell/IDE and choose notepad++ as the editor, since this process was torture to me, I thought as long as it can work I will never never change anything.

But, when I try to print the data in weather_info.txt,they are all mess code.So here comes the question: How to display chinese while running python script? That's a miserable story that I don't want to recall. You won't to know how much I had been suffering, the long hard journey of finding the correct display of data broke my heart.

I can show you the solution:

Put the following code before any line of python script,it means the declaration of the code:

# -*-coding utf-8 -*_

file = open('weather_info.txt','r',encoding = 'utf-8')

or this way (it comes from classmate @yanzhiw)

# -*- coding: utf-8 -*-
import codecs
f = codecs.open('weather_info.txt', 'r','utf-8')

The reason why you have to put this code, I try to analysis in simple words:

  1. The string inside Python is encoded in the format of unicode.
  2. The type of output string depends on the system, most of window IDE use (utf-8、gbk、cp936、GB2312 or ascii) to decode the string, that's why the python script can't be print correctly.
  3. The way to converse the str and the unicode object, is to encode and decode with unicode.Unicode can be the "bridge" to decode and to encode. such as: str1.decode('gb2312'),it means to converte the str(encoded by gb2312)to unicode,the opposite way is str2.encode('gb2312').
  4. I use powershell to run python script,the defaut encode of PS is 936GBK, differs from "utf-8",which encodes the str of the weather_info.txt,so that's why chinese can be displayed while runs the python script directlly,but display mess code while reading chinese inside the text.

It seems simple,right? It took me almost 10 hours to read a bunch of dazzling characters,and try to understand the basic concepts of encode and deconde,and why people have to invente them.

Not only that,the most important thing I learn from these is:

  • How to ask a question correctly ?

The very beginning,I google "How python chinese mess code"? Obviously I got nothing,so I changed my questiong to "How to display utf-8 in windows console?" That's better, although it didn't offer much help. I just googled around to try my luck, I even read the Q AND A in some geekers blog-which cost me so much time...

  • The way python3 to solute this problem is different from python2,so Prefix Code :

    -# coding=utf-8 from _future _ import unicode_literals

is useless!!

  • So the question shoule be accurately expressed as: "powershell run python 3 py file to read the document within the Chinese, how to do with mess code?"

Anyway, Now I bought Macbookpro for a while, I still don't have the heart to install python and sublime in Mbp...


OK,As a beginner, here something you need to know:

  1. While adding new functions, remember these rules:

    (1)The first character of function can’t be a number; (2)The first line of the function definition is called the header; the rest is called the body. The header has to end with a colon and the body has to be indented. the indentation is always four spaces.This space is different from the Tab space.

  2. Three ways to import modules: import xxx(math) or from xxx(math) import yyy(pi), and from xxx(math) import * (the last one use the star operator to import everything from the module)

  3. Everytime you def a function,watch the logic of every word every str,remember to put them in suitable place.

What I mention above is sosososo important!(I won't tell you how many times I fell on this DEEP DIP).

results matching ""

    No results matching ""