2011-04-05 82 views
0

我有兩個文本文件:1)緯度值的單個數組,2)經度值的單個數組。文本文件#1中有94個緯度值,文本文件#2中有192個經度值。我想做一個1對1的映射,以便有94個緯度×192經度= 18048個座標對。我需要保持個別值的準確精度,所以認爲寫成字符串是必要的。矩陣,Python腳本的緯度,經度座標對

通過研究,我認爲有必要將兩個文件讀入(「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

呃,TLDR。縮短列表 – Anycorn 2011-04-05 04:30:14

回答

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 =和尾;)。

編輯:尾隨;可能留在輸入文件中。

+0

謝謝。這就是我所追求的。但是腳本插入了一個「;」在緯度對之間: – 2011-04-05 13:26:36

+0

@Matt也許你已經離開了尾部';'在兩個文件的結尾?讓我編輯片段以接受'';' – zindel 2011-04-05 13:30:54

+0

@Matt,現在嘗試plz。 – zindel 2011-04-05 13:33:59

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) 
+0

我還將輸入列表封裝爲python列表,例如:lon = [0,1.875,...];讓我知道什麼格式的輸入文件是一個更好的答案。 – 2011-04-05 04:41:45

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]