Marinaio
Marinaio

Reputation: 111

looping through a file with python

New to python and stuck on reading a file...

I want to search a file for data using python3.

I have a data file that looks like this:

hostname,timestamp,#of CPUs,memory,cpu,disk

hostname1,07311906,1,4.84%,74%,0.45%
hostname2,07311906,2,3.84%,24%,0.45%
hostname1,07311907,1,4.85%,74%,0.49%
hostname2,07311907,2,4.64%,44%,0.30%
hostname1,07311908,1,5.20%,74%,0.78%
hostname2,07311908,2,4.44%,54%,0.40%

I'd like to cycle through a config file like this pseudo code:

for i in server.list do
   <graph the data server i for the month of `date %m`>
 done

The end goal is loop through my data file and do processing for each server in a list.

Upvotes: 0

Views: 36

Answers (1)

Kevin M&#252;ller
Kevin M&#252;ller

Reputation: 770

You can use with open() in python like this:

with open(filename, 'r') as fileContent:
    listOfLines = fileContent.readlines()

Now you will have a list of every line inside of the file. Probably also helpful would be:

for row in listOfLines:
    curData = row.split(',')

This will split the content of the row at every "," and return a list. After that you can work with the data.

Upvotes: 1

Related Questions