gpt4 book ai didi

Python pandas 检查行是否包含字符串

转载 作者:行者123 更新时间:2023-12-04 04:09:48 24 4
gpt4 key购买 nike

我正在尝试制作一个程序,用包含哈希和电子邮件的 CSV 文件对找到的密码哈希进行排序。我正在尝试从 ex.csv 获取“电子邮件”,从散列值一致的 found.txt 获取“通过”。但是我收到一个错误 - raise ValueError(
ValueError:DataFrame 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

我的代码-

import pandas as pd
import numpy as np

ex = pd.read_csv("ex.csv",delimiter=",")
found = pd.read_csv("found.txt",delimiter=":")

temp = ex[["Hash","Email"]]
te = found[["Hash","Pass"]]

for index,row in te.iterrows(): #Looping through file
if temp.loc[temp['Hash'] == row['Hash'][index]]: # If pandas can't locate Hash string inside a first file, list is empty. And I am comparing that here
print(temp['Email'][index]) # If successful, print out the
print(te['Pass'][index]) # found values in the console

来自 ex.csv 的示例:

                                          Hash                    Email
0 210ac64b3c5a570e177b26bb8d1e3e93f72081fd example@example.com
1 707a1b7b7d9a12112738bcef3acc22aa09e8c915 example@example.com
2 24529d87ea25b05daba92c2b7d219a470c3ff3a0 example@example.com

来自 found.txt 的示例:

                                         Hash         Pass
0 f8fa3b3da3fc71e1eaf6c18e4afef626e1fc7fc1 pass1
1 ecdc5a7c21b2eb84dfe498657039a4296cbad3f4 pass2
2 f61946739c01cff69974093452057c90c3e0ba14 pass3

或者也许有更好的方法来遍历行并检查该行是否包含来自另一个文件行的字符串? ;)

最佳答案

import pandas as pd
import numpy as np

ex = pd.read_csv("c.csv",delimiter=",")
found = pd.read_csv("d.csv",delimiter=",")

print(ex)
print(found)

temp = ex[['Hash','Email']]
te = found[['Hash','Pass']]

for temp1, temp2 in zip(te.iterrows(), temp.iterrows()):
if temp2[1]['Hash'][temp2[0]] == temp1[1]['Hash'][temp1[0]]:
print(temp['Email'][temp2[0]])
print(te['Pass'][temp1[0]])

我有这样的存储值

1)c.csv

Hash,Email
210ac64b3c5a570e177b26bb8d1e3e93f72081fd,example@example.com
707a1b7b7d9a12112738bcef3acc22aa09e8c915,example@example.com
24529d87ea25b05daba92c2b7d219a470c3ff3a0,example@example.com

2) 数据.csv

Hash,Pass
f8fa3b3da3fc71e1eaf6c18e4afef626e1fc7fc1,pass1
ecdc5a7c21b2eb84dfe498657039a4296cbad3f4,pass2
f61946739c01cff69974093452057c90c3e0ba14,pass3

关于Python pandas 检查行是否包含字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61937206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com