首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据逐行读入Python中作为列表

将数据逐行读入Python中作为列表
EN

Stack Overflow用户
提问于 2017-09-25 03:36:17
回答 2查看 393关注 0票数 0

我想在一系列的坐标中,用它们的精度,变成一个三角剖分函数,提供三角坐标。我已经能够使用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.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)

每一种方法都是一组坐标和精度,以输入三角剖分函数。文本文档将它们按行分隔开来。

这是我试图将文本文件读取到的三角剖分函数:

代码语言:javascript
复制
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语句测试函数时,它可以工作。但是,当我尝试将数据从文本文件加载到函数中时,如下所示:

代码语言:javascript
复制
with open(filename, 'r') as file:
   for points in file:
       triangulation(points)

我得到了错误:IndexError: string index out of range。我理解这是因为它不是作为一个列表读取,而是作为一个字符串读取,但是当我试图将它转换为list对象points = list(points)时,它也不能被识别为一个包含不同坐标的列表。我的问题是如何将文件读入python,以便将其转换为在三角函数中工作。

EN

回答 2

Stack Overflow用户

发布于 2017-09-25 03:55:12

正如@FMCorz所建议的,您应该使用JSON或其他一些机器可读的格式。

这样做很简单,只需以任何机器可读的格式将点列表转储到文本文件中,然后再将其读入。

下面是一个最小的示例(使用JSON):

代码语言:javascript
复制
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))

如果您想使用列表列表(包含点列表的列表),可以这样做:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-09-25 03:56:12

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

现在,这当然是您可以做的事情,但是最好只使用print()以外的其他东西。也就是说,使用一组不同的规则将数据转换为字符串,在这种规则中,人们已经编写了代码来逆转流程。您可以使用的一种常见格式是JSON,它包含用来进行转换的库。其他可以处理数值数据的格式包括CSV (这里是Python模块)和HDF5 (由外部图书馆支持,可能对您的情况来说太过了)。关键是,您需要为数据和字符串之间的转换选择一些规则,并在两个方向上使用相应的代码。在最初的示例中,您只使用了从数据到字符串的规则,并期望Python猜测返回的规则。

如果您想更多地阅读这方面的内容,那么将数据转换为字符串(或者,实际上,将其转换为可以放在文件中的内容)的过程称为格式化或序列化,这取决于上下文,而将字符串转换回原始数据的反向过程称为解析或反序列化。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46397243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档