为了使处理文件时用 utf-8 编码,做如上编码声明
1 2 3 4 5
   | import hashlib def md5(text):     m = hashlib.md5()     m.update(text)     return m.hexdigest()
   | 
 
利用 hashlib 进行 md5 编码
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
   | def import_points_csv(filename,outfilename):     print(filename)     g=file(outfilename,"w+")          with open(filename, 'r') as f:              line = f.readline()                  while line:             line = line.replace('n','')             line = line.replace('r','')                          row = line.split(',')                          state = row[0].strip()             date = row[20]             thistype = '国控'             print(thistype)             name = row[1].strip()             stationid = md5(name+state+thistype)             row[21] = stationid             md5id = md5(date+stationid)             row[22] = md5id             row[1]                          print(row)             for i in range(0, len(row)-1):                 row[i] = row[i] + ','                          g.writelines(row)             g.writelines('n')                          line = f.readline()     g.close()
   | 
 
1
   | import_points_csv('国控2.csv'.decode('utf-8').encode('gbk'),'contry_2.csv')
  | 
 
由于文件名是中文,因此要将其编码才能被程序读取,不然在运行时显示文件名为乱码且无法读取