如何对csv文件中的特定列进行哈希处理?

时间:2019-04-20 16:48:56

标签: python csv hash

我试图对第2列和第8列进行哈希处理,但最终对整个文件进行哈希处理。我的代码有什么问题?

import csv
import hashlib


with open('UserInfo.csv') as csvfile:

    with open('UserInfo_Hashed.csv', 'w') as newfile:

        reader = csv.DictReader(csvfile)

        for r in reader:

            hashing = hashlib.sha256((r['Password']).encode('utf-8')).hexdigest()

            newfile.write(hashing + '\n')

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

由于您的代码仅显示尝试对Password列进行哈希处理,因此以下代码仅对Password列进行哈希处理。

import csv
import hashlib

with open('UserInfo.csv') as csvfile:

    with open('UserInfo_Hashed.csv', 'w') as newfile:

        reader = csv.DictReader(csvfile)

        for i, r in enumerate(reader):
            #  writing csv headers
            if i is 0:
                newfile.write(','.join(r) + '\n')

            # hashing the 'Password' column
            r['Password'] = hashlib.sha256((r['Password']).encode('utf-8')).hexdigest()

            # writing the new row to the file with hashed 'Password'
            newfile.write(','.join(r.values()) + '\n')

您的代码存在问题newfile.write(hashing + '\n'),因为这仅将哈希密码写入文件(没有其他列)。另外,您也没有将CSV标头写入新文件。


我强烈建议使用Pandas

import pandas as pd
import hashlib

# reading CSV input
df = pd.read_csv('UserInfo.csv')

# hashing the 'Password' column
df['Password'] = df['Password'].apply(lambda x: \
        hashlib.sha256(x.encode('utf-8')).hexdigest())

# writing the new CSV output
df.to_csv('UserInfo_Hashed.csv', index=False)