简体   繁体   English

使用python3读取txt文件

[英]Read txt files with python3

I've collected the GPS position data of my day into a txt file. 我已将当天的GPS位置数据收集到txt文件中。 There are N lines about my data. 关于我的数据有N行。 And there are 5 fields in one line(latitude, longitude, altitude, date, time). 一行中有5个字段(纬度,经度,海拔高度,日期,时间)。 I want to use python read that. 我想用python读取。 This is my code(There is no problem in this code). 这是我的代码(此代码没有问题)。

 #open file
 f = open("path",'r')
 #read lines
 lines = f.read()
 print(lines)
 #close files
 f.close()

How can I read it like latitude, longitude or date, time? 如何阅读纬度,经度或日期,时间等信息? I just want one of the data in one line. 我只想要一行中的数据之一。

Assuming the fields are split by commas you could do something like this 假设用逗号分隔字段,则可以执行以下操作

with open("path", "r") as file:
    for line in file.readlines():
        latitude, longitude, altitude, date, time = line.split(",")
        do_something(latitude, longitude, altitude, date, time)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM