问题
I have a file with x,y,z values. I wish to find an elegant way to open and add a new value id to each line and save again the same file.
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)
ex
1 1 10
2 2 10
3 3 10
the id will be
get_point_grid_id(1,1,0,10,1,1)
(9, 1)
get_point_grid_id(2,2,0,10,1,1)
(8, 2)
get_point_grid_id(3,3,0,10,1,1)
(7, 3)
the new file will be
1 1 10 (9, 1)
2 2 10 (8, 2)
3 3 10 (7, 3)
i am reading in Stackoverflow several approach and i tested several approaches. i am honest to say that i have tried and failed to save the new file.
i had tried the followig solution
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),0,10,1,1)
element = [x,y,z,id]
newelement = " ".join([str(e) for e in element])+ "\n"
f.write(newelement)
but i get this error message
Traceback (most recent call last):
File "<editor selection>", line 3, in <module>
ValueError: too many values to unpack
where newelement (real data) is
'481499.55 6244324.75 19.15 (377, 2909)\n'
回答1:
You can emulate the required behavior via the fileinput module but bear in mind that it will create a backup copy of your original 10GB+ file in the background:
#! /usr/bin/env python
import fileinput
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)
input_file = "test.dat"
#
# Add mode='rb' to the arguments of fileinput.input() if you are
# using a binary file on operating systems that differentiate
# between binary and text files (e.g. Microsoft Windows).
#
for line in fileinput.input(input_file, inplace=True):
columns = line.split()
if 3 == len(columns):
x, y, z = columns
id = get_point_grid_id(float(x),float(y),0,10,1,1)
print "{0} {1} {2} {3}".format(x, y, z, id)
The inplace
parameter passed to fileinput.input
triggers the magic.
来源:https://stackoverflow.com/questions/15030038/python-read-a-file-save-a-new-column-for-each-line-ad-save-the-same-file