2017-09-25 164 views
0

我想讀一系列的座標以及它們的準確性轉化爲三角函數來提供三角座標。我已經能夠使用Python來創建一個包含座標列表中爲每個三角.txt的文件即將數據讀入Python一行一行地作爲列表

[(-1.2354798, 36.8959406, -22.0), (-1.245124, 36.9027361, -31.0), (-1.2387697, 36.897921, -12.0), (-1.3019762, 36.8923956, -4.0)]

[(-1.3103075, 36.8932163, -70.0), (-1.3017684, 36.8899228, -12.0)]

[(-1.3014139, 36.8899931, -34.0), (-1.2028006, 36.9180461, -54.0), (-1.1996497, 36.9286186, -67.0), (-1.2081047, 36.9239936, -22.0), (-1.2013893, 36.9066869, -11.0)]

每個那些將座標和準確性的一組喂到三角功能。文本文件按行分開。

這是三角功能我想讀的文本文件導入:

def triangulate(points): 
    """ 
    Given points in (x,y, signal) format, approximate the position (x,y). 

    Reading: 
    * http://stackoverflow.com/questions/10329877/how-to-properly-triangulate-gsm-cell-towers-to-get-a-location 
    * http://www.neilson.co.za/?p=364 
    * http://gis.stackexchange.com/questions/40660/trilateration-algorithm-for-n-amount-of-points 
    * http://gis.stackexchange.com/questions/2850/what-algorithm-should-i-use-for-wifi-geolocation 
    """ 
    # Weighted signal strength 
    ws = sum(p[2] for p in points) 
    points = tuple((x,y,signal/ws) for (x,y,signal) in points) 

    # Approximate 
    return (
     sum(p[0]*p[2] for p in points), # x 
     sum(p[1]*p[2] for p in points) # y 
    ) 


print(triangulate([ 
    (14.2565389, 48.2248439, 80), 
    (14.2637736, 48.2331576, 55), 
    (14.2488966, 48.232513, 55), 
    (14.2488163, 48.2277972, 55), 
    (14.2647612, 48.2299558, 21), 
])) 

當我測試與它的工作原理上面打印語句的功能。但是,當我嘗試從文本文件中的數據加載到功能如下」

with open(filename, 'r') as file: 
    for points in file: 
     triangulation(points) 

我得到的錯誤:IndexError: string index out of range我明白,這是因爲它沒有被讀取作爲一個名單,但作爲字符串,但當我嘗試將其轉換爲列表對象points = list(points)它也不被識別爲不同的座標列表。我的問題是我應該如何讀取文件到python中,以便它被轉換爲在三角函數內工作

+0

您的文本文件按原樣讀取,Python不知道您的行的結構。它沒有看到列表和元組。一個快速的解決辦法是做'data = eval(points)',但我強烈建議不要這樣做,因爲'eval'通常是不鼓勵的。相反,您應該重新格式化您的文件以包含JSON或任何其他機器可讀格式。 – FMCorz

回答

1

正如@FMCorz所建議的,您應該使用JSON或其他機器可讀格式。

這樣做很簡單,只需在任何機器可讀的格式貴點的列表轉儲到文本文件,然後再閱讀它回來的事

這裏是(使用JSON)一個小例子:

import json 

def triangulate(points): 
    """ Given points in (x,y, signal) format, approximate the position (x,y). 

     Reading: 
     * http://stackoverflow.com/questions/10329877/how-to-properly-triangulate-gsm-cell-towers-to-get-a-location 
     * http://www.neilson.co.za/?p=364 
     * http://gis.stackexchange.com/questions/40660/trilateration-algorithm-for-n-amount-of-points 
     * http://gis.stackexchange.com/questions/2850/what-algorithm-should-i-use-for-wifi-geolocation 
    """ 
    # Weighted signal strength 
    ws = sum(p[2] for p in points) 
    points = tuple((x,y,signal/ws) for (x,y,signal) in points) 

    # Approximate 
    return (
     sum(p[0]*p[2] for p in points), # x 
     sum(p[1]*p[2] for p in points) # y 
    ) 

points = [(14.2565389, 48.2248439, 80), 
      (14.2637736, 48.2331576, 55), 
      (14.2488966, 48.232513, 55), 
      (14.2488163, 48.2277972, 55), 
      (14.2647612, 48.2299558, 21)] 

with open("points.txt", 'w') as file: 
    file.write(json.dumps(points)) 

with open("points.txt", 'r') as file: 
    for line in file: 
     points = json.loads(line) 
     print(triangulate(points)) 

如果你想用一個列表的列表(含點的名單列表),你可以做這樣的事情:

import json 

def triangulate(points): 
    """ Given points in (x,y, signal) format, approximate the position (x,y). 

     Reading: 
     * http://stackoverflow.com/questions/10329877/how-to-properly-triangulate-gsm-cell-towers-to-get-a-location 
     * http://www.neilson.co.za/?p=364 
     * http://gis.stackexchange.com/questions/40660/trilateration-algorithm-for-n-amount-of-points 
     * http://gis.stackexchange.com/questions/2850/what-algorithm-should-i-use-for-wifi-geolocation 
    """ 
    # Weighted signal strength 
    ws = sum(p[2] for p in points) 
    points = tuple((x,y,signal/ws) for (x,y,signal) in points) 

    # Approximate 
    return (
     sum(p[0]*p[2] for p in points), # x 
     sum(p[1]*p[2] for p in points) # y 
    ) 


points_list = [[(-1.2354798, 36.8959406, -22.0), (-1.245124, 36.9027361, -31.0), (-1.2387697, 36.897921, -12.0), (-1.3019762, 36.8923956, -4.0)], 
       [(-1.3103075, 36.8932163, -70.0), (-1.3017684, 36.8899228, -12.0)], 
       [(-1.3014139, 36.8899931, -34.0), (-1.2028006, 36.9180461, -54.0), (-1.1996497, 36.9286186, -67.0), (-1.2081047, 36.9239936, -22.0), (-1.2013893, 36.9066869, -11.0)]] 

with open("points.txt", 'w') as file: 
    file.write(json.dumps(points_list)) 

with open("points.txt", 'r') as file: 
    for line in file: 
     points_list = json.loads(line) 
     for points in points_list: 
      print(triangulate(points)) 
+0

嘿謝謝你的回覆。我嘗試在創建列表的位置重新創建工作,並在執行JSON轉儲並嘗試將其讀回函數時,出現以下錯誤:解碼 raise JSONDecodeError(「Extra data」,s,end ) json.decoder.JSONDecodeError:額外數據:第1行第130列(char 129) – chaimocha

+0

是否因爲JSON文件將原始文件中的所有參數設置爲括號? – chaimocha

+0

你是怎麼寫這個清單的? 如果您有多個要寫入的點列表,您應該對列表列表進行循環寫入或單獨寫入每個列表。 我用另一個例子編輯了我的答案。 –

1

什麼你從文件中得到的是一個字符串,但是Python沒有按」不知道應該如何解釋這個字符串。它可以是元組列表的打印表示形式,就像你的情況一樣,但它也可能是一本書的一部分,或者它可能是一些壓縮數據,等等。猜測如何處理從文件讀取的字符串不是語言的工作。這是你的工作;您必須編寫一些代碼來獲取這些字符串並解析它們 - 也就是說,將它們轉換爲程序所需的數據,並使用用於將數據轉換爲字符串的規則的反轉。

現在,這當然是你可以做的事情,但它可能更好地使用print()以外的東西。也就是說,使用一組不同的規則將數據轉換爲字符串,其中一個人已經編寫了代碼以反轉該過程。您可以使用的通用格式是JSON,Python包含a library to do the conversions。其他可用於數字數據的格式包括CSV(此處爲the Python module)和HDF5(由an external library支持,可能是針對您的案例的矯枉過正)。問題是,您需要選擇一些規則來在數據和字符串之間進行轉換,並使用兩個方向中的相應代碼。在你原來的例子中,你只是使用規則從數據到字符串,並期望Python猜測返回的規則。

如果您想了解更多關於這一點,數據轉換爲字符串(或者,真的,東西可以放在一個文件)的過程稱爲格式化系列化,根據上下文,以及轉換的字符串返回到原始數據的逆過程被稱爲解析反序列化

相關問題