網頁設計

當前位置 /首頁/設計製作/網頁設計/列表

python輕鬆實現程式碼編碼格式轉換

文章主要介紹了python概率計算器實現方法,例項分析了Python實現概率計算的技巧,具有一定參考借鑑價值,需要的朋友可以參考下.

python輕鬆實現程式碼編碼格式轉換

本文例項講述了python概率計算器實現方法。分享給大家供大家參考。具體實現方法如下:

?

1

2

3

4

5

6

7

8

9

由於某些原因,需要將程式碼從A機房遷移到B機房,這兩個之間不能互相訪問,但是歷史原因導致A機房的程式碼全是utf8編碼的,B機房要求是GBK編碼,看看這個怎麼解決。雖然很簡單,但是還是要推薦給大家,需要的小夥伴參考下吧。

最近剛換工作不久,沒太多的時間去整理工作中的東西,大部分時間都在用來熟悉新公司的業務,熟悉他們的程式碼框架了,最主要的是還有很多新東西要學,我之前主要是做php後臺開發的,來這邊之後還要把我半路出家的前端學好、還要學習C++,哈哈,總之很充實了,每天下班回家都可以睡的很香(一句話總結,就是吃得香、睡的香~)。再說說換工作時候吧,今年年初正式畢業半年了,感覺自己技術增長很快,原公司裡面程式設計師的地位還不如運營,所以想換個工作,面試了3家(2家大的、一家小的),都給offer了,當然從大公司裡面挑了個各方面綜合(工資、幹什麼、交通等等)還不錯的,反正感覺就很順利的進來了(比畢業的時候容易多了),哈哈,越努力、越幸運,越幸運、越努力!。從這周開始,繼續整理部落格,免得給自己造成懶得習慣。

剛來這個公司,熟悉了環境,老大就開始讓我做一個遷移、修改程式碼的工作,我想說的是,這種工作真沒勁~~,看別人的程式碼、改別人的程式碼、這裡改個變數、那裡改個檔名······,都是些沒技術含量、很繁瑣的事情,不過通過遷移程式碼順便熟悉下環境也好。扯了這麼多,說說今天的`主題吧——程式碼編碼格式改變,由於某些原因,需要將程式碼從A機房遷移到B機房,這兩個之間不能互相訪問,但是歷史原因導致A機房的程式碼全是utf8編碼的,B機房要求是GBK編碼,看看這個怎麼解決。

編碼問題

先說說為什麼會有編碼問題,就拿上面那個例子來說,B機房這邊資料庫全是GBK編碼的,因此從資料庫中取出來的資料都是GBK的,從資料庫中取出來的資料是GBK編碼的,要在展示的時候不亂碼,在不對資料庫取出的資料轉換的情況下,就需要傳送header的時候設定編碼為GBK,輸出的檔案(html、tpl等)都必須是GBK的,看看下面這個圖會更清楚點:

DB(GBK) => php等(編碼格式不限但如果程式碼檔案中有漢字,檔案就要是gbk編碼或者在漢字輸出的時候轉化為gbk) => header(GBK) => html、tpl(GBK)

或者還有一種方式只在出庫的時候在程式碼中將utf8轉化為gbk,總的來說utf8還是更流行點,問題更少點

DB(GBK) => php等(utf8,並將從資料庫取出的資料轉化為utf8) => header(utf8) => html、tpl(utf8)

只要按照上面這兩種規範編碼格式,就不會出現亂碼情況,起碼我測試的第一種方式是沒問題的,所以我猜第二種也ok,好了,現在就來寫一個轉換檔案編碼格式的小指令碼:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

#!/usr/bin/python

# -*- coding: utf-8 -*-

#Filename:

import os

import sys

def ChangeEncode(file,fromEncode,toEncode):

try:

f=open(file)

s=()

e()

u=de(fromEncode)

s=de(toEncode)

f=open(file,"w");

e(s)

return 0;

except:

return -1;

def Do(dirname,fromEncode,toEncode):

for root,dirs,files in (dirname):

for _file in files:

_file=(root,_file)

if(ChangeEncode(_file,fromEncode,toEncode)!=0):

print "[轉換失敗:]"+_file

else:

print "[成功:]"+_file

def CheckParam(dirname,fromEncode,toEncode):

encode=["UTF-8","GBK","gbk","utf-8"]

if(not fromEncode in encode or not toEncode in encode):

return 2

if(fromEncode==toEncode):

return 3

if(not r(dirname)):

return 1

return 0

if __name__=="__main__":

error={1:"第一個引數不是一個有效的資料夾",3:"源編碼和目標編碼相同",2:"您要轉化的編碼不再範圍之內:UTF-8,GBK"}

dirname=[1]

fromEncode=[2]

toEncode=[3]

ret=CheckParam(dirname,fromEncode,toEncode)

if(ret!=0):

print error[ret]

else:

Do(dirname,fromEncode,toEncode)

指令碼很簡單,使用也很簡單

程式碼如下:

./ target_dir fromEncode toEncode

這裡要注意下,幾種常見編碼的關係:

us-ascii編碼是utf-8編碼的一個子集,這個是從stackoverflow上得到的,原文如下ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded,

我試了下確實是的,在不加漢字的時候顯示編碼為us-ascii,加了漢字之後,變為utf-8。

還有就是ASNI編碼格式,這代表是本地編碼格式,比如說在簡體中文作業系統下,ASNI編碼就代表GBK編碼,這點還需要注意

還有一點就是一個在linux下檢視檔案編碼格式的命令是:

程式碼如下:

file -i *

可以看到檔案的編碼格式。

當然了,上面的可能有些檔案中有特殊字元,處理的時候會失敗,但一般程式檔案是沒有問題的。

以上就是本文所述的全部內容了,希望對大家學習python能夠有所幫助。

請您花一點時間將文章分享給您的朋友或者留下評論。我們將會由衷感謝您的支援!

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

from random import randrange

#randrange form random module

def calc_prob(strengths):

"""A function that receives an array of two numbers

indicating the strength of each party

and returns the winner"""

if strengths[1]>strengths[0]:

#Bring the bigger number to the first position in the array

temp=strengths[0]

strengths[0]=strengths[1]

strengths[1]=temp

prob1=abs(strengths[0]-strengths[1])

#The relative strength of the 2 parties

prob2=randrange(0,100)

#To calculate the luck that decides the outcome

if prob2 in range(0,33-prob1):

#Check if the weaker party is capable of winning.

#The condition gets narrower with the increase

#in relative strengths of each parties

return strengths[1]

elif prob2 in range(33-prob1,66-prob1):

#The middle condition

return "Draw"

else:

return strengths[0]

#Luck favors the stronger party and if relative strength

#between the teams is too large,

#the match ends up in favor of the stronger party

#Example

calc_prob([50,75]);#Always has to be a list to allow exchange

#Can be programmed in hundreds of better ways. Good luck!

希望本文所述對大家的Python程式設計有所幫助。