python, replacing certain word into other data

I'm having a problem in replacing my data. I have a large dataset. lets say I have 15 attributes, the 15th attributes is Label. I want to change if the data contains botnet, it will change the whole data into "1". for example the data botnet are here or we are looking for botnet, both data will replace to "1"

I've already try using replace

x = "botnet is here and im looking for botnet"

tags = ['botnet']

for tag in tags:
    x = x.replace(tag,'')
print(x)

This code only replce word "botnet" But what I want is if the data contain botnet it will change the whole sentences to "1"

Upvotes: 2

Views: 162

Answers (3)

Ishara Dayarathna
Ishara Dayarathna

Reputation: 3601

Try this:

label = ['botnet is here' , 'im looking for botnet', 'just a test']
tags='botnet'
for x in label:
    if tags in x:
        label[label.index(x)]='1'
print(label)

output: only sentences contain 'botnet' are replaced

['1', '1', 'just a test']

Upvotes: 1

kaleem231
kaleem231

Reputation: 357

You should assign string value to tags variable instead of list.
Iterate x not tags.
Try this code.

x = "botnet is here and im looking for botnet"
tags='botnet'
for tag in x.split():
    if tag==tags:
        x=tag.replace(tag,'1')
print(x)

output of this code is.

1

Upvotes: 0

Michael
Michael

Reputation: 2414

for tag in tags:
    if tag in x:
        x = "1"
        break

Upvotes: 1

Related Questions