0

I am able to plot from a simple csv like this using matplot:

1  2
2  4
3  8
4  16
5  32
.
.

The values are seperated by tab. Now I need to read the data from a csv which looks like this:

    #  Name Test Number1                                      \\Name of the csv
#Sometimes there is a comment which has one line
# or even more

    Category1  Number2  Test  Temperature  Voltage            \\Labels for the plot
    # [1]  [1/min]  [s]  [°C]  [mV]                           \\Units
    MinMax  2.3  5  9  48  22                                 \\Data starts here
    MinMax  9.87  6.01  8  9  3
    MinMax  1  2  3  4  5
    MinMax  99.52  5  8  6.66  0

How can I get the data from my csv and the Labels for the plots? For example if I want to plot Test and Temperature? Thereis a huge amount of rows and columns.

Thank you!

GM18
  • 13
  • 4
  • Duplicate of http://stackoverflow.com/questions/13545388/plot-data-from-csv-file-with-matplotlib – Shijo Jan 05 '17 at 13:49
  • Thank you for the link, somehow I didn´t find this. The problem in this thread was, that the header was exactly 10 lines long. In my csv I don´t know how many lines I need to skip. Furthermore I want to get the line with the labels. I am not sure if genfromtxt is suitable for my problem. – GM18 Jan 05 '17 at 15:10
  • If you know the header labels , read the data line by line and skip all lines until you match line with header – Shijo Jan 05 '17 at 15:12
  • I dont know the header labels. I only know the line with the header labels starts with a blank. How can I search each line untill I find the line which starts with a blank? – GM18 Jan 05 '17 at 15:18
  • blank is not an ideal value to look for , I see that in your data, first line starts with a space ' # Name Test Number1 ' – Shijo Jan 05 '17 at 15:23
  • I am not interested in the first line, I need to extract the line with the Labels and the line with the units (and of course the data). The number of lines and columns isn´t the same in every csv I need to plot, so I can´t just use 'skiprows'. – GM18 Jan 05 '17 at 15:39

1 Answers1

1
import csv

with open('path\to\sample.txt', 'r') as csvfile:

csvreader = csv.reader(csvfile, delimiter='\t')
foundheader=False
for row in csvreader:
    if row[0].startswith(' '):
        foundheader=True
    if foundheader:
        print row

sample data for testing

#Name Test Number1 
#Sometimes there is a comment which has one line
#or even more
# Name Test Number1 \\Name of the csv
#Sometimes there is a comment which has one line
# or even more
 Category1  Number2 Test    Temperature Voltage 
 #[1]   [1/min] [s] [°C]    [mV]    
 MinMax 2.3 5   9   48  22  
 MinMax 9.87    6.01    8   9   3
 MinMax 1   2   3   4   5
 MinMax 99.52   5   8   6.66    0

output

[' Category1', 'Number2', 'Test', 'Temperature', 'Voltage', '']
[' #[1]', '[1/min]', '[s]', '[\xc2\xb0C]', '[mV]', '']
[' MinMax', '2.3', '5', '9', '48', '22', '']
[' MinMax', '9.87', '6.01', '8', '9', '3']
[' MinMax', '1', '2', '3', '4', '5']
[' MinMax', '99.52', '5', '8', '6.66', '0']
Shijo
  • 9,313
  • 3
  • 19
  • 31
  • Thank you Shijo! Unfortunately I can´t upvote, cause I don´t have enough reputation. – GM18 Jan 05 '17 at 15:43