I know that np.loadtxt allows you to quickly extract data from columns in a text file, and I know that you can use skiprows
to skip the first N number of rows. Can you specify an end line number, such that np.loadtxt only extracts text between two known line numbers in a text file?
In the following example, you can specify:
(dat1,dat2) = np.loadtxt(file, skiprows = 1, usecols = (0,1), unpack=True)
but I get an error saying that "ValueError: could not convert string to float: Data1"
Example: 1 Data1 Data2 Data3 2 1 3 5 3 7 1 6 [...] 48 8 9 2 49 2 7 6 50 Data1 Data2 Data3 51 5 6 1 52 9 12 3 53 1 0 2
np.genfromtxt
with
skip_header : int, optional
The number of lines to skip at the beginning of the file.
skip_footer : int, optional
The number of lines to skip at the end of the file.
max_rows : int, optional
The maximum number of rows to read. Must not be used with skip_footer
at the same time. If given, the value must be at least 1. Default is
to read the entire file.
gives a lot of control over which lines are read.
Another option is to open the file yourself, eg
with open('myfile', 'rb') as f:
<skip>
np.genfromtxt(f, ...)
etc
and pass it to genfromtxt
. You can even feed it line by line. genfromtxt
(and loadtxt
) is happy with anything that feeds it lines - a file, a list of lines, a generator of lines, etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.