简体   繁体   English

如何散列 csv 文件中的特定列?

[英]How do I hash specific columns from a csv file?

I'm trying to hash column 2 and 8 but I ended up hashing the entire file.我正在尝试对第 2 列和第 8 列进行哈希处理,但最终对整个文件进行了哈希处理。 What's the issue with my code?我的代码有什么问题?

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')

在此处输入图像描述

在此处输入图像描述

Since your code showing your attempt to hash the Password column only, the following code just does the hashing for the Password column.由于您的代码显示您仅尝试对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')

The issue with your code is with this line newfile.write(hashing + '\n') , as this writes only the hashed password to the file (without the other columns).您的代码的问题在于这一行newfile.write(hashing + '\n') ,因为这只将散列密码写入文件(没有其他列)。 Also you didn't write the CSV header to the new file.此外,您没有将 CSV 标头写入新文件。


I strongly suggest using Pandas :我强烈建议使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Python 中的 csv 文件中获取具有相同名称的特定列 - How do I get specific columns with the same name from a csv file in Python 如何将 csv 文件中的特定列导入 python? - How can I import specific columns from csv file to python? 如何从 Python 中的 CSV 文件中抓取特定数据? - How do I scrape specific data from a CSV file in Python? 如何将列从一个 CSV 文件复制到另一个 CSV 文件? - How do I copy columns from one CSV file to another CSV file? 如何在csv文件中的行中打印特定字段,以及如何将输入内容写入csv文件? - How do I print specific fields from rows in a csv file and how to write input to a csv file? 如何在打印一定范围内的值时读取CSV文件中的特定行和列 - How do I read specific rows AND columns in CSV file while printing values within a certain range 如何读取文件中的特定列并将它们变成新的 csv - How do I read specific columns in a file and make them into a new csv 如何 plot 或在 CSV 文件中显示特定列? - How Can I plot or show specific Columns in a CSV File? 如何从 Python 中的 a.csv 文件中仅读取特定列和特定行? - How do I read only a specific column and a specific row from a .csv file in Python? 如何仅将csv文件中的特定列加载到DataFrame中 - How to load only specific columns from csv file into a DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM