简体   繁体   English

python:我如何比较 doc A 和 doc B,如果它在 doc B 中找到匹配的字符串,我该如何打印整行?

[英]python: How do I compare doc A and doc B and if it finds a string that matches in doc B how do I print the entire line?

I have document A:我有文件A:

['5a0cd3b5-4249-bf6f-d009-17a81532660e', '7e44fc1b-44fa-cdda-8491-f8a5bca1cfa3', 'daa73753-4b56-9d21-d73e-f3b3f4c9b1a6', 'f7425a39-43ca-e1fe-5b2b-56a51ed479c5']

I have document B:我有文件 B:

abc 5a0cd3b5-4249-bf6f-d009-17a81532660e
def CDA41B87-4D3A-C17C-5F6D-8990CC9C5EFB
ghi Odin 157BCEBE-484D-82E2-2A60-C8B4B11197EA
jkl 72E724E9-4BA4-2D12-CE1A-8DB1A528B9D3
mno 9E648B20-4ED5-1F34-87A9-979CBE9A958A

If the IDs in document A match with IDs from document B, how can you print the entire line?如果文档 A 中的 ID 与文档 B 中的 ID 匹配,您如何打印整行?

Exemple: This ID '5a0cd3b5-4249-bf6f-d009-17a81532660e' from doc A is found in doc B at the first line: 'abc 5a0cd3b5-4249-bf6f-d009-17a81532660e'.示例:文档 A 中的此 ID '5a0cd3b5-4249-bf6f-d009-17a81532660e' 位于文档 B 的第一行:'abc 5a0cd3b5-4249-bf6f-d009-17a8153266 (print the first line) (打印第一行)

I've tried using panda:我试过使用熊猫:

import pandas as pd
df1 = pd.read_csv('shopid.txt', header=None, names=['id'])
df2 = pd.read_csv('skin_database2.txt', header=None, names=['id', 'name'], delim_whitespace=True)

res = df2[df2['name'].isin(df1['id'].unique())]
print(res)

I've also tried this:我也试过这个:

response = requests.request("GET", url, headers=headers, data=payload)
    data = response.json()["SkinsPanelLayout"]["SingleItemOffers"]
dataLog = []
with open('skin_database2.txt', 'rt') as f:
    data = f.readlines()
for line in data:
    if line.__contains__(data):
        print(line)
        dataLog.append(line)
print(dataLog)

But I get this error:但我收到此错误:

    if line.__contains__(data):
TypeError: 'in <string>' requires string as left operand, not list

is there a way to use a list/variable?有没有办法使用列表/变量?

Using inputs:使用输入:

Shopid.txt:店铺编号.txt:

['157BCEBE-484D-82E2-2A60-C8B4B11197EA', '7e44fc1b-44fa-cdda-8491-f8a5bca1cfa3', '65BAA0CD-42EC-F99D-54A0-338D795B5824', 'f7425a39-43ca-e1fe-5b2b-56a51ed479c5']

Otherfile.txt:其他文件.txt:

Glitchpop Odin,97AF88E4-4176-9FA3-4A26-57919443DAB7
dot EXE Odin,5A0CD3B5-4249-BF6F-D009-17A81532660E
Prime//2.0 Odin,157BCEBE-484D-82E2-2A60-C8B4B11197EA
Prism III Odin,72E724E9-4BA4-2D12-CE1A-8DB1A528B9D3
Smite Odin,9E648B20-4ED5-1F34-87A9-979CBE9A958A
Sensation Odin,65BAA0CD-42EC-F99D-54A0-338D795B5824
Lightwave Odin,57523CF0-4574-968B-9F17-168E3BDB6D0D
Standard Odin,F7425A39-43CA-E1FE-5B2B-56A51ED479C5
  • Mention: in order for pandas to read rows correctly, you need to format the inputs as I did.提到:为了让熊猫正确读取行,您需要像我一样格式化输入。 (the shopid.txt must have each entry on separate rows whereas the other must have name and id separated by ,) (shopid.txt 的每个条目都必须在单独的行上,而另一个条目的名称和 ID 必须用 , 分隔)

Pandas solution revised:熊猫解决方案修订:

import pandas as pd

with open('shopid.txt', 'r') as f:
    in_list_formatted = str(f.read()).replace(
        '[', '').replace(']', '').replace('\'', '').split(',')
    inputs = [i.strip().upper() for i in in_list_formatted]

df1 = pd.DataFrame(inputs, columns=['id'])
df2 = pd.read_csv('skin_database2.txt', header=None, names=[
                  'name', 'id'])

found_list = []
for item in df1['id']:
    found = df2.loc[df2['id'] == item]
    if found.empty:
        continue
    found_list.append(found)
output = pd.concat(found_list, ignore_index=True)
print(output)

Output:输出:

              name                                    id
0  Prime//2.0 Odin  157BCEBE-484D-82E2-2A60-C8B4B11197EA
1   Sensation Odin  65BAA0CD-42EC-F99D-54A0-338D795B5824
2    Standard Odin  F7425A39-43CA-E1FE-5B2B-56A51ED479C5

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

相关问题 如何在 Linux 上查看 Python 文档? - How do I see the Python doc on Linux? 在使用etree的Python中,我如何保留doc类型和声明 - In Python using etree how do I retain the doc type and declaration 如何使用python搜索和替换ms word doc中所有出现的字符串? - How do I search & replace all occurrences of a string in a ms word doc with python? 我如何做python unittest doc推荐的懒惰导入方法? - How do I do python unittest doc's recommended method of lazy import? 在 spacy 中:在 spacy doc (python) 中添加一个 span (doc[a:b]) 作为实体 - In spacy: Add a span (doc[a:b]) as entity in a spacy doc (python) 如何使用Python将新数据附加到现有doc / docx文件 - How do I append new data to existing doc/docx file using Python 如何编写可读取doc / docx文件并将其转换为txt的python脚本? - How do I write a python script that can read doc/docx files and convert them to txt? 如何使用 Python 从 doc/docx 文件中提取数据 - How do I extract data from a doc/docx file using Python 如何将python输出导出到GoogleSheets或Excel Doc? (首选表格) - How do I export my python output to GoogleSheets or an Excel Doc? (Sheets Preferred) 如何以编程方式更新python文件中的文档字符串 - How to update a doc string in python files programatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM