简体   繁体   English

使用Python进行数据分析-读取文本文件

[英]Data Analysis using Python--Reading Text File

I have a text file structure like: 我有一个文本文件结构,如:

X   Y   Home
X   Y   School

X and Y are times and the last column is a location. X和Y是时间,最后一列是位置。 How can I use python to assess the X and Y element where the 3rd Column is Home? 我如何使用python评估第三列为Home的X和Y元素?

Note: I have already opened the text file. 注意:我已经打开了文本文件。 If you feel I am asking to much, I would appreciate a link to a place that tells how to do something like this. 如果您觉得我有很多要求,那么希望您能链接到一个告诉您如何执行此操作的地方。 Also, X and Y are not numbers they are strings: ex. 此外,X和Y都不是数字,它们是字符串:例如。 4:30 AM 上午4:30

Assuming the contents of the file have been dumped to a variable, one way is to parse every line like so: 假设文件的内容已转储到变量中,一种方法是像这样解析每一行:

for line in file_content:
     columns = line.split()
     x_1 = columns[0]
     x_2 = columns[1]
     home = columns[2]
     ...
     ...

Basically the split distinguishes X, Y, Home and School and places them into a list. 基本上, split将X,Y,家庭和学校区分开,并将它们放入列表中。 So ultimately, columns is a list containing [X, Y, Home] regardless of the type of data. 因此,最终, columns是一个包含[X, Y, Home]的列表[X, Y, Home]而与数据类型无关。

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

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