我有以下contacts.txt 文件:
0, 0.989246526088, 0.762584552317, 0.989246526088, 0.989246526088, 5, 2, 20, 91, 114, 127
1, 0.832366089749, 0.67518676348, 0.832366089749, 0.832366089749, 3, 6, 24, 114
2, 0.923079422275, 0.798673866486, 0.923079422275, 0.923079422275, 5, 0, 65, 73, 91, 114
3, 0.0820269441841, 0.879379910489, 0.0820269441841, 0.0820269441841, 3, 71, 91, 120
4, 0.449863833595, 0.985883232333, 0.449863833595, 0.449863833595, 3, 16, 69, 104
6, 0.887055481253, 0.623261413511, 0.887055481253, 0.887055481253, 5, 1, 25, 87, 100, 114
7, 0.111156294437, 0.255444048959, 0.111156294437, 0.111156294437, 3, 19, 83, 111
9, 0.514040361142, 0.373030232483, 0.514040361142, 0.514040361142, 4, 38, 59, 72, 76
11, 0.597169587765, 0.0286747230467, 0.597169587765, 0.597169587765, 3, 56, 101, 108
12, 0.89754811115, 0.361667992685, 0.89754811115, 0.89754811115, 3, 86, 92, 126
13, 0.571528472894, 0.860250953547, 0.571528472894, 0.571528472894, 5, 30, 79, 82, 101, 104
14, 0.593696200969, 0.680733858699, 0.593696200969, 0.593696200969, 3, 78, 103, 124
依此类推,16383 或 16384 行,具体取决于文件。
我尝试了以下代码
with open('contacts.dat') as infile:
n, x, y, z, radius, contact_number = [[int(num) for num in line.strip().split()[:5]] for line in infile]
neighbours = [[int(num) for num in line.strip().split()[5:]] for line in infile]
它似乎不起作用。
我正在寻找的是,对于文件的每一行,将以下存储到不同的数组中:
每行
- 第零个元素 -->
n
(其中 --> 表示“存储在数组中”) - 第一个元素 -->
x
- 第二个元素-->
y
- 第三个元素 -->
z
- 第四个元素 -->
radius
- 第五个元素 -->
contact_number
- 该行的剩余元素(如果有)-->
neighbours