Monday, July 4, 2022

Files

Files may contain structured, semistructured, and unstructured data. Python’s built-in open() function allows you to open a file so you can use its data within your script. However, depending on the format of the data (for example, CSV, JSON, or XML), you may need to import a corresponding library to be able to perform read, write, and/or append operations on it.

Plaintext files don’t require a library to be further processed and are simply considered as sequences of lines in Python. As an example, look at the following message that a Cisco router might send to a logfile:

dat= 'Jul 19 10:30:37'

host='sm1-prt-highw157'

syslogtag='%SYS-1-CPURISINGTHRESHOLD:'

msg=' Threshold: Total CPU Utilization(Total/Intr): 17%/1%,

Top 3 processes(Pid/Util): 85/9%, 146/4%, 80/1%'

You’ll be able to read this line by line, looking for the required information. Thus, if your task is to find messages that include information about CPU utilization and extract particular figures from it, your script should recognize the last line in the snippet as a message to be selected.

Share:

0 comments:

Post a Comment