将数据逐行读取到Python中作为列表

时间:2017-09-25 03:36:18

标签: python text

我想将一系列坐标及其准确度读入三角测量函数,以提供三角坐标。我已经能够使用python创建一个.txt文档,其中包含每个三角测量的坐标列表,即

  

[( - 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),
]))

当我使用上面的print语句测试函数时,它可以工作。但是当我尝试将文本文件中的数据加载到函数中时,如下所示“

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

我收到错误:IndexError: string index out of range。我知道这是因为它不是作为列表读入而是作为字符串读入,但是当我尝试将其转换为列表对象points = list(points)时,它也不会被识别为不同坐标的列表。我的问题是如何将文件读入python,以便将其翻译为在三角函数中工作。

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分:1)

你从文件中得到的是一个字符串,但是Python并不知道应该如何解释该字符串。它可以是元组列表的打印表示,就像你的情况一样,但它也可以是一本书的一部分,或者它可能是一些压缩数据,等等。猜测如何处理从文件中读取的字符串并不是语言的工作。这是你的工作;你必须编写一些代码来获取这些字符串并解析它们 - 也就是说,将它们转换为程序所需的数据,使用与首先将数据转换为字符串的规则相反的方式。

现在,这肯定是你可以做的事情,但是使用.profile之外的其他东西可能更好。也就是说,使用一组不同的规则将数据转换为字符串,人们已经编写了代码来反转过程。您可以使用的通用格式是JSON,其中Python包含a library to do the conversions。其他可以处理数字数据的格式包括CSV(此处为the Python module)和HDF5(由an external library支持,可能对您的情况有些过分)。关键是,您需要选择一组用于在数据和字符串之间进行转换的规则,并在两个方向中使用相应的代码。在您的原始示例中,您只使用规则从数据转换为字符串并期望Python猜测返回的规则。

如果您想了解更多相关内容,将数据转换为字符串(或者实际上是可以放入文件中的内容)的过程称为格式化序列化< / em>,取决于上下文,将字符串转换回原始数据的相反过程称为解析反序列化