我有两个文本文件:1)单个纬度值数组,2)单个经度值数组。文本文件#1中有94个纬度值,文本文件#2中有192个经度值。我想做1到1的映射,这样就有94个纬度x 192经度= 18048个坐标对。我需要保持各个值的精确精度,因此认为写入字符串是必要的。
通过研究我认为有必要将两个文件(“rb”)读入内存,关闭它们并打开一个名为“latlon.txt”的可写(“wb”)新文件。最终的格式是csv(lat,lon + / n)。我不知道如何在python中编写脚本。以下是帮助说明我的问题的值:
lon = 0, 1.875, 3.75, 5.625, 7.5, 9.375, 11.25, 13.125, 15, 16.875, 18.75,
20.625, 22.5, 24.375, 26.25, 28.125, 30, 31.875, 33.75, 35.625, 37.5,
39.375, 41.25, 43.125, 45, 46.875, 48.75, 50.625, 52.5, 54.375, 56.25,
58.125, 60, 61.875, 63.75, 65.625, 67.5, 69.375, 71.25, 73.125, 75,
76.875, 78.75, 80.625, 82.5, 84.375, 86.25, 88.125, 90, 91.875, 93.75,
95.625, 97.5, 99.375, 101.25, 103.125, 105, 106.875, 108.75, 110.625,
112.5, 114.375, 116.25, 118.125, 120, 121.875, 123.75, 125.625, 127.5,
129.375, 131.25, 133.125, 135, 136.875, 138.75, 140.625, 142.5, 144.375,
146.25, 148.125, 150, 151.875, 153.75, 155.625, 157.5, 159.375, 161.25,
163.125, 165, 166.875, 168.75, 170.625, 172.5, 174.375, 176.25, 178.125,
180, 181.875, 183.75, 185.625, 187.5, 189.375, 191.25, 193.125, 195,
196.875, 198.75, 200.625, 202.5, 204.375, 206.25, 208.125, 210, 211.875,
213.75, 215.625, 217.5, 219.375, 221.25, 223.125, 225, 226.875, 228.75,
230.625, 232.5, 234.375, 236.25, 238.125, 240, 241.875, 243.75, 245.625,
247.5, 249.375, 251.25, 253.125, 255, 256.875, 258.75, 260.625, 262.5,
264.375, 266.25, 268.125, 270, 271.875, 273.75, 275.625, 277.5, 279.375,
281.25, 283.125, 285, 286.875, 288.75, 290.625, 292.5, 294.375, 296.25,
298.125, 300, 301.875, 303.75, 305.625, 307.5, 309.375, 311.25, 313.125,
315, 316.875, 318.75, 320.625, 322.5, 324.375, 326.25, 328.125, 330,
331.875, 333.75, 335.625, 337.5, 339.375, 341.25, 343.125, 345, 346.875,
348.75, 350.625, 352.5, 354.375, 356.25, 358.125 ;
lat = 88.5419501372975, 86.6531671712612, 84.7532299278375,
82.8507724666685, 80.9473590380955, 79.0434830262188, 77.1393485086043,
75.2350550220443, 73.3306568580251, 71.4261861337297, 69.5216630592775,
67.6171009860561, 65.7125090882981, 63.8078938782031, 61.9032601063951,
59.9986113201436, 58.0939502220327, 56.1892789076074, 54.2845990270979,
52.3799118980861, 50.4752185856401, 48.5705199603727, 46.6658167412098,
44.7611095273708, 42.8563988226079, 40.9516850538099, 39.0469685854431,
37.1422497308786, 35.2375287613668, 33.33280591321, 31.4280813935466,
29.523355385052, 27.6186280497899, 25.7138995323903, 23.8091699626917,
21.9044394579537, 19.9997081247232, 18.0949760604204, 16.1902433546966,
14.285510090607, 12.3807763456323, 10.4760421925768, 8.57130770036727,
6.6665729347709, 4.76183795905001, 2.85710283456696, 0.952367621352339,
-0.952367621352339, -2.85710283456696, -4.76183795905001,
-6.6665729347709, -8.57130770036727, -10.4760421925768,
-12.3807763456323, -14.285510090607, -16.1902433546966,
-18.0949760604204, -19.9997081247232, -21.9044394579537,
-23.8091699626917, -25.7138995323903, -27.6186280497899,
-29.523355385052, -31.4280813935466, -33.33280591321, -35.2375287613668,
-37.1422497308786, -39.0469685854431, -40.9516850538099,
-42.8563988226079, -44.7611095273708, -46.6658167412098,
-48.5705199603727, -50.4752185856401, -52.3799118980861,
-54.2845990270979, -56.1892789076074, -58.0939502220327,
-59.9986113201436, -61.9032601063951, -63.8078938782031,
-65.7125090882981, -67.6171009860561, -69.5216630592775,
-71.4261861337297, -73.3306568580251, -75.2350550220443,
-77.1393485086043, -79.0434830262188, -80.9473590380955,
-82.8507724666685, -84.7532299278375, -86.6531671712612, -88.5419501372975 ;`enter code here`
答案 0 :(得分:2)
from itertools import product
# This is just a scrap code to quickly read the values in the files, you can replace it with whatever suites you
lat=open("lat.txt",'r').read().replace("lat =","").replace(";","").replace(" ","").replace("\n","").split(',')
lon=open("lon.txt",'r').read().replace("lon =","").replace(";","").replace(" ","").replace("\n","").split(',')
#this is the important part of the code
for coordinate in product( lat,lon):
print coordinate[0],", ",coordinate[1]
答案 1 :(得分:1)
lon = [l.strip() for l in open('lon.txt').read().replace(';', '').split(',') if l.strip()]
lat = [l.strip() for l in open('lat.txt').read().replace(';', '').split(',') if l.strip()]
with open('lonlat.txt', 'wb') as f:
lonlat = "".join(["%s,%s\n" % (lo, la) for lo in lon for la in lat])
f.write(lonlat)
该代码假定lon.txt
和lat.txt
是简单的逗号分隔列表(就像您的示例中一样,但没有前导var =
和尾随;
)。< / p>
修改:结尾;
可以保留在输入文件中。
答案 2 :(得分:0)
import csv
db = [[latitude, longitude] for latitude in lat for longitude in lon]
output = open('db.csv', 'w')
csvout = csv.writer(output)
csvout.writerows(db)