我需要将多个 ASCII 文件的cellsize和xllcorner转换为. 我一直在尝试在 ASCII 文件的标题中覆盖它们,就像使用常规文本文件一样,如下所示:yllcornermkm
for rw_file in os.listdir(r"C:\Users\Marie\Test"):
rw_file_path = os.path.join(r"C:\Users\Marie\Test", rw_file)
with open(rw_file_path, 'r+') as f:
# skip the first two lines of the header
f.readline()
f.readline()
# convert the values of cellsize, xllcorner and yllcorner into km
line3 = f.readline()
header_x, xllcorner = line3.split()
xllcorner_new = int(xllcorner) / 1000
f.seek(2)
f.write(re.sub(header_x, xllcorner_new)) #third argument??
line4 = f.readline()
header_y, yllcorner = line4.split()
yllcorner_new = int(yllcorner) / 1000
f.seek(3)
f.write(re.sub(header_y, yllcorner_new))
line5 = f.readline()
header_size, cellsize = line5.split()
cellsize_new = int(cellsize) / 1000
f.seek(4)
f.write(re.sub(header_size, cellsize_new))
但是当然函数 re.sub 需要三个参数。我不知道该怎么做。我仍然是初学者,所以我确信有一个简单的方法,但我找不到它。我可以以某种方式覆盖标题中的这些行,还是有其他方法?
慕容708150
相关分类