![](/img/trans.png)
[英]delimiter [0001] in a text file, reading using np.loadtxt in python
[英]Using np.loadtxt between two specific lines (Python 2.7)
我知道np.loadtxt讓您快速在文本文件中提取列的數據,我知道,你可以用skiprows
跳過行的第N個。 您是否可以指定結束行號,以便np.loadtxt僅提取文本文件中兩個已知行號之間的文本?
在以下示例中,您可以指定:
(dat1,dat2) = np.loadtxt(file, skiprows = 1, usecols = (0,1), unpack=True)
但我收到一條錯誤消息,提示“ ValueError:無法將字符串轉換為float:Data1”
示例: 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
與
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.
可以很好地控制讀取哪些行。
另一種選擇是自己打開文件,例如
with open('myfile', 'rb') as f:
<skip>
np.genfromtxt(f, ...)
etc
並將其傳遞給genfromtxt
。 您甚至可以逐行喂食。 genfromtxt
(和loadtxt
)對任何能loadtxt
都感到滿意-文件,行列表,行生成器等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.