欧美日韩不卡一区二区三区,www.蜜臀.com,高清国产一区二区三区四区五区,欧美日韩三级视频,欧美性综合,精品国产91久久久久久,99a精品视频在线观看

網(wǎng)頁設(shè)計(jì)

python輕松實(shí)現(xiàn)代碼編碼格式轉(zhuǎn)換

時(shí)間:2025-03-18 04:05:41 網(wǎng)頁設(shè)計(jì) 我要投稿
  • 相關(guān)推薦

python輕松實(shí)現(xiàn)代碼編碼格式轉(zhuǎn)換

  文章主要介紹了python概率計(jì)算器實(shí)現(xiàn)方法,實(shí)例分析了Python實(shí)現(xiàn)概率計(jì)算的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下.

  本文實(shí)例講述了python概率計(jì)算器實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

  ?

  1

  2

  3

  4

  5

  6

  7

  8

  9

  由于某些原因,需要將代碼從A機(jī)房遷移到B機(jī)房,這兩個(gè)之間不能互相訪問,但是歷史原因?qū)е翧機(jī)房的代碼全是utf8編碼的,B機(jī)房要求是GBK編碼,看看這個(gè)怎么解決。雖然很簡單,但是還是要推薦給大家,需要的小伙伴參考下吧。

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

  剛來這個(gè)公司,熟悉了環(huán)境,老大就開始讓我做一個(gè)遷移、修改代碼的工作,我想說的是,這種工作真沒勁~~,看別人的代碼、改別人的代碼、這里改個(gè)變量、那里改個(gè)文件名······,都是些沒技術(shù)含量、很繁瑣的事情,不過通過遷移代碼順便熟悉下環(huán)境也好。扯了這么多,說說今天的主題吧——代碼編碼格式改變,由于某些原因,需要將代碼從A機(jī)房遷移到B機(jī)房,這兩個(gè)之間不能互相訪問,但是歷史原因?qū)е翧機(jī)房的代碼全是utf8編碼的,B機(jī)房要求是GBK編碼,看看這個(gè)怎么解決。

  編碼問題

  先說說為什么會(huì)有編碼問題,就拿上面那個(gè)例子來說,B機(jī)房這邊數(shù)據(jù)庫全是GBK編碼的,因此從數(shù)據(jù)庫中取出來的數(shù)據(jù)都是GBK的,從數(shù)據(jù)庫中取出來的數(shù)據(jù)是GBK編碼的,要在展示的時(shí)候不亂碼,在不對數(shù)據(jù)庫取出的數(shù)據(jù)轉(zhuǎn)換的情況下,就需要發(fā)送header的時(shí)候設(shè)置編碼為GBK,輸出的文件(html、tpl等)都必須是GBK的,看看下面這個(gè)圖會(huì)更清楚點(diǎn):

  DB(GBK) => php等(編碼格式不限但如果代碼文件中有漢字,文件就要是gbk編碼或者在漢字輸出的時(shí)候轉(zhuǎn)化為gbk) => header(GBK) => html、tpl(GBK)

  或者還有一種方式只在出庫的時(shí)候在代碼中將utf8轉(zhuǎn)化為gbk,總的來說utf8還是更流行點(diǎn),問題更少點(diǎn)

  DB(GBK) => php等(utf8,并將從數(shù)據(jù)庫取出的數(shù)據(jù)轉(zhuǎn)化為utf8) => header(utf8) => html、tpl(utf8)

  只要按照上面這兩種規(guī)范編碼格式,就不會(huì)出現(xiàn)亂碼情況,起碼我測試的第一種方式是沒問題的,所以我猜第二種也ok,好了,現(xiàn)在就來寫一個(gè)轉(zhuǎn)換文件編碼格式的小腳本:

  ?

  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 "[轉(zhuǎn)換失敗:]"+_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:"第一個(gè)參數(shù)不是一個(gè)有效的文件夾",3:"源編碼和目標(biāo)編碼相同",2:"您要轉(zhuǎn)化的編碼不再范圍之內(nèi):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

  這里要注意下,幾種常見編碼的關(guān)系:

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

  我試了下確實(shí)是的,在不加漢字的時(shí)候顯示編碼為us-ascii,加了漢字之后,變?yōu)閡tf-8。

  還有就是ASNI編碼格式,這代表是本地編碼格式,比如說在簡體中文操作系統(tǒng)下,ASNI編碼就代表GBK編碼,這點(diǎn)還需要注意

  還有一點(diǎn)就是一個(gè)在linux下查看文件編碼格式的命令是:

  代碼如下:

  file -i *

  可以看到文件的編碼格式。

  當(dāng)然了,上面的可能有些文件中有特殊字符,處理的時(shí)候會(huì)失敗,但一般程序文件是沒有問題的。

  以上就是本文所述的全部內(nèi)容了,希望對大家學(xué)習(xí)python能夠有所幫助。

  請您花一點(diǎn)時(shí)間將文章分享給您的朋友或者留下評論。我們將會(huì)由衷感謝您的支持!

  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程序設(shè)計(jì)有所幫助。

【python輕松實(shí)現(xiàn)代碼編碼格式轉(zhuǎn)換】相關(guān)文章:

使用python實(shí)現(xiàn)Linux異步epoll的代碼10-27

PHP如何實(shí)現(xiàn)Unicode和Utf-8編碼相互轉(zhuǎn)換07-28

Python怎么實(shí)現(xiàn)多行注釋08-24

php數(shù)組基于dom實(shí)現(xiàn)轉(zhuǎn)換xml格式數(shù)據(jù)08-27

利用python實(shí)現(xiàn)簡單爬蟲功能09-25

實(shí)現(xiàn)c語言中字符串和數(shù)字的相互轉(zhuǎn)換的代碼06-30

php中將時(shí)間差轉(zhuǎn)換為字符串提示的實(shí)現(xiàn)代碼09-03

PHP滾動(dòng)日志的代碼實(shí)現(xiàn)11-15

PHP編碼轉(zhuǎn)換函數(shù)應(yīng)用技巧詳解08-26