<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>
            網頁設計

            python輕松實現代碼編碼格式轉換

            時間:2025-03-18 04:05:41 網頁設計 我要投稿
            • 相關推薦

            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:changeEncode.py

              import os

              import sys

              def ChangeEncode(file,fromEncode,toEncode):

              try:

              f=open(file)

              s=f.read()

              f.close()

              u=s.decode(fromEncode)

              s=u.encode(toEncode)

              f=open(file,"w");

              f.write(s)

              return 0;

              except:

              return -1;

              def Do(dirname,fromEncode,toEncode):

              for root,dirs,files in os.walk(dirname):

              for _file in files:

              _file=os.path.join(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 os.path.isdir(dirname)):

              return 1

              return 0

              if __name__=="__main__":

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

              dirname=sys.argv[1]

              fromEncode=sys.argv[2]

              toEncode=sys.argv[3]

              ret=CheckParam(dirname,fromEncode,toEncode)

              if(ret!=0):

              print error[ret]

              else:

              Do(dirname,fromEncode,toEncode)

              腳本很簡單,使用也很簡單

              代碼如下:

              ./changeEncode.py 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程序設計有所幫助。

            【python輕松實現代碼編碼格式轉換】相關文章:

            使用python實現Linux異步epoll的代碼10-27

            PHP如何實現Unicode和Utf-8編碼相互轉換07-28

            Python怎么實現多行注釋08-24

            php數組基于dom實現轉換xml格式數據08-27

            利用python實現簡單爬蟲功能09-25

            實現c語言中字符串和數字的相互轉換的代碼06-30

            php中將時間差轉換為字符串提示的實現代碼09-03

            PHP滾動日志的代碼實現11-15

            PHP編碼轉換函數應用技巧詳解08-26

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看