【问题标题】:Converting data lines columns to values in python将数据行列转换为python中的值
【发布时间】:2013-08-04 05:01:46
【问题描述】:

我如何使用 enemerate 或其他一些函数在我输入到 python 的整个字符串的 txt 文件中返回这些值

TW11 42.83 -72.94   2.1

TW22 41.727 -75.81 3.9

我目前的工作是

in_file =open('tow.txt','r')

for line in_infile:
    L=line.strip().split()
    Tower = L[0]
    Lat = L[1]
    Long = L[2]
    ComDis = L[3]

print (Tower,Lat,Long,ComDis)

但是我只能返回输入文件的第一行,文件中大约有 20 行只是想举一个简短的例子

【问题讨论】:

    标签: python string split int strip


    【解决方案1】:

    在 python 中,缩进很重要。您只打印最后一行。如果您想打印每一行,请通过缩进将您的打印内容移到 for 外观中:

    in_file =open('tow.txt','r')
    
    for line in_infile:
        L=line.strip().split()
        Tower = L[0]
        Lat = L[1]
        Long = L[2]
        ComDis = L[3]
    
        print (Tower,Lat,Long,ComDis)
    

    【讨论】:

    • 非常感谢!你知道我是如何只打印整个输出的第一行的,我正在尝试 Tower[1] 或 Tower[0:4] 并且它只给了我该行的前 4 个字母?
    • 我不明白,你想从文件中打印第一行,或者你想为每一行打印 Tower?
    • 我明白了,我刚刚做了 """print Tower """ 并打印了所有的值,直下 22 行
    • 刚刚问了第二部分作业的另一个问题,如果你有时间,也许你也可以帮忙
    猜你喜欢
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    相关资源
    最近更新 更多