简体   繁体   中英

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:

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

I have document 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?

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'. (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:

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

Otherfile.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 ,)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM