从矩阵生成纬度和经度坐标对的Python脚本

0 投票
3 回答
1905 浏览
提问于 2025-04-16 15:06

我有两个文本文件:第一个文件里是一个纬度值的数组,第二个文件里是一个经度值的数组。第一个文件有94个纬度值,第二个文件有192个经度值。我想把它们一一对应起来,这样就会有94个纬度和192个经度的组合,总共18048对坐标。我需要保持每个值的精确度,所以我觉得把它们写成字符串是必要的。

经过研究,我认为需要以“rb”的方式读取这两个文件到内存中,然后关闭它们,再打开一个新的文件,以“wb”的方式写入,命名为“latlon.txt”。最终的格式是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`

3 个回答

0

在编程中,有时候我们会遇到一些问题,可能是因为代码写得不够好,或者是我们对某些概念理解得不够透彻。这时候,查找相关的资料或者在社区提问就显得特别重要。StackOverflow就是一个很好的地方,大家可以在这里互相帮助,分享经验。

当你在StackOverflow上提问时,记得要把问题描述清楚,这样其他人才能更好地理解你的困惑,并给出有效的建议。同时,查看别人提问的方式也能帮助你学习如何更好地表达自己的问题。

总之,编程是一条不断学习的路,遇到问题时不要气馁,积极寻求帮助,才能更快地进步。


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)
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
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.txtlat.txt 是简单的用逗号分隔的列表(就像你给的例子那样,不过开头的 var = 和结尾的 ; 可以省略)。

补充说明: 输入文件中可以保留结尾的 ;

撰写回答