How to read only certain rows and cells from csv with Python pandas?

I have csv-file with this structure:

Last Name   First Name  Start Date  End Date            
Example     Eva         1.1.2021    15.6.2021
                                        
Here is some random information.                                        
                                        
------- Header-------                       
Index   Date    Time        Reading
0   10.4.2021   16:26:01    0,1             
1   10.4.2021   16:25:44    0,1             
2   10.4.2021   16:00:00    0,1             
3   10.4.2021   16:00:00    0,1             
4   10.4.2021   14:00:00    0,1             
5   10.4.2021   14:00:00    0,1             
6   10.4.2021   13:00:00    0,3             

------- Header------- 
Index   Date    Time        Reading
0   10.4.2021   16:26:01    0,1             
1   10.4.2021   16:25:44    0,1             
2   10.4.2021   16:00:00    0,1             
3   10.4.2021   16:00:00    0,1             
4   10.4.2021   14:00:00    0,1             
5   10.4.2021   14:00:00    0,1             
6   10.4.2021   13:00:00    0,3

I want to read the file using pandas and make a dictionary about the data, like this for example: {'last_name': 'Example', 'first_name': 'Eva'} and so on. How can I read certain values into variables for example? At the moment, I read the csv -file like this: data = pd.read_csv(file, sep='delimiter').

Answer

header

If you only want to read the beginning of the file as a dictionary, you can do:

pd.read_csv('filename.csv', sep='ss+', nrows=1).loc[0].to_dict()

output:

{'Last Name': 'Example',
 'First Name': 'Eva',
 'Start Date': '1.1.2021',
 'End Date': '15.6.2021'}

rest of the file

To read the rest of the file:

df = (pd.read_csv('filename.csv',
                  sep='s+',
                  skiprows=6,
                  index_col=0,
                 )
        .drop(['Index', '-------']) # get rid of extra headers
     )

output:

            Date      Time Reading
Index                             
0      10.4.2021  16:26:01     0,1
1      10.4.2021  16:25:44     0,1
2      10.4.2021  16:00:00     0,1
3      10.4.2021  16:00:00     0,1
4      10.4.2021  14:00:00     0,1
5      10.4.2021  14:00:00     0,1
6      10.4.2021  13:00:00     0,3
0      10.4.2021  16:26:01     0,1
1      10.4.2021  16:25:44     0,1
2      10.4.2021  16:00:00     0,1
3      10.4.2021  16:00:00     0,1
4      10.4.2021  14:00:00     0,1
5      10.4.2021  14:00:00     0,1
6      10.4.2021  13:00:00     0,3

If you need to determine programmatically the number of lines to skip:

with open('filename.csv') as f:
    skip = 1
    for l in f:
        if l.startswith('-------'):
            break
        skip+=1

skip: 6