python find text in a string from a csv

Viewed 39

I open a csv file that is an output form a windows CMD command.

I read the csv and assigne a variable, but I can find any word even I'm seeing it.

IT seems to be enconde...

content of output.txt ( Host Name: hostname

OS Name: Microsoft Windows Server 2016 Standard

OS Version: 10.0.14393 N/A Build 14393

OS Manufacturer: Microsoft Corporation)

f = open("output.txt", "rt")
f = f.read()
print(f.find("OS Name"))

OUTPUT

<class 'str'>

-1

1 Answers

Try this :

f = open("output.txt", "rt", encoding='utf-8')
f = f.read()
print(f.find("OS Name"))

The code above returns 21 corresponding to the position of the first occurence of the value "OS Name" while print(f.find("random_value")) returns -1 since "random_value" does not exist in the text file.

Related