Home > Mobile >  Skip first several rows when plotting a CSV-file
Skip first several rows when plotting a CSV-file

Time:09-29

For a project at work I'm working on a code that reads a csv-file and generates a plot.
My problem is, I work with multiple csv-files but all of them contain 10-40 rows in the beginning, filled with device and sensor information.
I would wish for my code to detect where the first line of values are and to start reading the values from there into my array. But since my experience with Python is very low, I couldnt find a good solution.
If you can recommend me specific methods or change my code, feel free to comment.
Thanks to everyone taking their time to help me

import matplotlib.pyplot as plt
import csv
  
a = []
b = []
c = []
d = []
e = []

with open('PATH','r') as csvfile:
    lines = csv.reader(csvfile, delimiter=',')
    for row in lines:
            a.append(float(row [0]))
            b.append(float(row [1]))
            #c.append(float(row [2]))
            #d.append(float(row [3]))
            #e.append(float(row [4]))
      
        
        
f = plt.figure()
f.set_figwidth(12)
f.set_figheight(8)

#plt.plot(X-Achse, Y-Achse, linewidth=* , color = '  ', label = "  ")
plt.plot(a, b, linewidth=0.35, color = 'b', label = "Sensor 1")
#plt.plot(a, c, linewidth=0.35, color = 'g', label = "Sensor 2")

plt.title('Pressure Report', fontsize = 20)
plt.xlabel('Time(s)')
plt.ylabel('Pressure(bar)')
plt.grid()
plt.legend()
plt.show()

CodePudding user response:

You can skip the lines using conditional statements as below:

count = 1
for row in lines:
    if (count < 10 and count > 40):
        a.append(float(row [0]))
        b.append(float(row [1]))

    count  = 1

CodePudding user response:

What is working for me are the following changes. It may not be the fastest and best solution, but it does what its supposed to.

import matplotlib.pyplot as plt
import csv
  
path = 'PATH'
line_number = 0
list_of_results = []
count = 1

a = []
b = []
c = []
d = []
e = []

with open(path, 'r') as read_obj:
    for line in read_obj:
        line_number  = 1
        if "0.000000000" in line:
            list_of_results.append((line_number))
            
firstline = list_of_results[0]-1


with open(path,'r') as csvfile:
    lines = csv.reader(csvfile, delimiter=',')
    for row in lines:
        if (count > firstline):
            a.append(float(row [0]))
            b.append(float(row [1]))
            #c.append(float(row [2]))
            #d.append(float(row [3]))
            #e.append(float(row [4]))
        
        count  = 1

f = plt.figure()
f.set_figwidth(12)
f.set_figheight(8)

#plt.plot(X-Achse, Y-Achse, linewidth=* , color = '  ', label = "  ")
plt.plot(a, b, linewidth=0.35, color = 'b', label = "Sensor 1")
#plt.plot(a, c, linewidth=0.35, color = 'g', label = "Sensor 2")

plt.title('Pressure Report', fontsize = 20)
plt.xlabel('Time(s)')
plt.ylabel('Pressure(bar)')

#plt.axis([x_min, x_max, y_min, y_max])
#plt.axis([350, 380, -6, 2.2])

plt.grid()
plt.legend()
plt.show()

  • Related