我有以下问题。我正在读取文件 x,y,z 为:
481492.93 6244326.24 26.56
481493.03 6244325.60 25.06
481493.17 6244324.68 22.89
481493.50 6244322.52 17.80
481492.84 6244327.05 27.84
481492.90 6244326.66 26.90
481492.86 6244327.16 27.45
481493.48 6244323.08 17.79
481492.80 6244327.80 28.30
481492.94 6244326.84 26.04
..........................
我希望在同一个文件上读取、修改和写入(不创建备份文件,因为原始文件超过 10GB)
481492.93 6244326.24 26.56 (375, 2902)
481493.03 6244325.60 25.06 (376, 2902)
481493.17 6244324.68 22.89 (377, 2902)
481493.50 6244322.52 17.80 (379, 2903)
481492.84 6244327.05 27.84 (375, 2902)
481492.90 6244326.66 26.90 (375, 2902)
481492.86 6244327.16 27.45 (374, 2902)
481493.48 6244323.08 17.79 (379, 2903)
481492.80 6244327.80 28.30 (374, 2902)
481492.94 6244326.84 26.04 (375, 2902)
..........................
我写了以下方法
def get_point_grid_id(x,y,x_min,y_max,x_dist,y_dist):
col = int((x - x_min)/x_dist)
row = int((y_max - y)/y_dist)
return (row, col)
with open(file_temp, "r+") as f:
for line in open(file_temp):
x,y,z = line.split()
id = get_point_grid_id(float(x),float(y),origin[0],origin[1],1,1)
element = [x,y,z,id]
newelement = " ".join([str(e) for e in element])+ "\n"
f.write(newelement)
当我运行该功能时,出现以下错误
Traceback (most recent call last):
File "<editor selection>", line 3, in <module>
ValueError: too many values to unpack
我想这是原始文件的连接问题
出现错误
>>> x,y,z = line.split()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ValueError: too many values to unpack
哪里line
奇怪
'481499.82 6244470.31 29.23 (231, 2909)\n'
代替 '481499.82 6244470.31 29.23\n'
从新文件运行print line
后我得到这个打印后使用for line in open(file_temp):
481499.98 6244494.02 34.14
481499.98 6244494.02 34.14 (208, 2909)
481499.96 6244471.05 33.39
481499.96 6244471.05 33.39 (231, 2909)
481499.95 6244471.27 33.46
481499.95 6244471.27 33.46 (230, 2909)
481499.98 6244473.84 32.72
481499.98 6244473.84 32.72 (228, 2909)
481499.98 6244474.07 32.70
481499.98 6244474.07 32.70 (228, 2909)
481499.97 6244474.28 32.93
481499.97 6244474.28 32.93 (227, 2909)
481499.88 6244474.40 34.35
481499.88 6244474.40 34.35 (227, 2909)