Reading data into numpy array from text file

后端 未结 1 1613
长发绾君心
长发绾君心 2020-12-03 05:33

I have a file with some metadata, and then some actual data consisting of 2 columns with headings. Do I need to separate the two types of data before using genfromtxt in num

相关标签:
1条回答
  • 2020-12-03 05:53

    If you don't want the first n rows, try (if there is no missing data):

    data = numpy.loadtxt(yourFileName,skiprows=n)
    

    or (if there are missing data):

    data = numpy.genfromtxt(yourFileName,skiprows=n)    
    

    If you then want to parse the header information, you can go back and open the file parse the header, for example:

    fh = open(yourFileName,'r')
    for i,line in enumerate(fh):
        if i is n: break
        do_other_stuff_to_header(line)
    fh.close()
    
    0 讨论(0)
提交回复
热议问题