Python中的哈希字符串

时间:2015-03-13 11:04:16

标签: python

我正在处理一个数据集,该数据集具有用户社交安全性#(没有破折号),我想将其用作唯一ID,但是想要散列(md5)字符串以保护身份用户。以下是数据集的示例:

 id        |     date     |     sale_id
 543875600    2014-03-22        a4395

希望输出看起来像这样或类似:

 id                               |     date     |     sale_id
 762be25b5c6eb20dd6c791840c01aa33    2014-03-22        a4395

我想使用python来解决,因为我目前正在使用python与数据聚合目录中的许多文件,所以这将是我将包含在以下代码中的其他代码:

 import glob
 files = glob.glob( '*.csv' )
 output="combined.csv"

 with open(output, 'w' ) as result:
      for thefile in files:
          f = [open(thefile).read()]
          for line in f:
              result.write( line )
  message = 'file created'
  print (message) 

2 个答案:

答案 0 :(得分:0)

技术答案首先......

反转字符串 - 可读方式:

>>> s = "abcde"
>>> "".join(reversed(s))
'edcba'

反转字符串 - 不太可读的方式:

>>> s = "abcde"
>>> s[::-1]
'edcba'

“争夺”一个字符串:

>>> import random
>>> l = list(s)
>>> random.shuffle(l)
>>> "".join(l)
'dacbe'

现在Chris Arena和Zlopez是对的:两者都不是有效的“保护”,你真的想要反对你的ID。

答案 1 :(得分:0)

>>> import hashlib
>>> id = "000000000"
>>> my_hash = hashlib.sha224(id).hexdigest()
>>> my_hash
'c34c462b2fb1982287dc9df575c03669b308301dbc3be6d62dd83536'

当然,如果你真的需要它,你可以md5,或者库中的any other hash function