栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python在当前试题目录下创建一个文本文件test.txt_python关于文件的编程题?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python在当前试题目录下创建一个文本文件test.txt_python关于文件的编程题?

题目如下:

 简化版解答

       

with open('car_data.txt','r',encoding='gbk') as file:
    list = file.readlines()  # 读取每一行
for i in list:
    list = i.split(',')  # 字符串分割,返回列表
    if(list[0] == '2:00' and list[2]>'31.30'):    # 筛选想要的数据
        print("时间:{} 车牌:{} 北纬:{} 东经:{}".format(list[0],list[1],list[2],list[3]),end='')

 

复杂版解答
import pandas as pd
import numpy as np
df = pd.Dataframe(data=None,columns=['时间','车牌','北纬','东经'])  # 创建空的dataframe
with open('car_data.txt','r',encoding='gbk') as file:
    list = file.readlines()
count = 0
for i in list:  # 将数据添加进dataframe中
    s = i.split(',')
    df.loc[count] = s
    count = count+1

df['北纬'] = df['北纬'].astype(float)  # 转换数据类型
df_new = df[(df['时间'] == '2:00') & (df['北纬'] > 31.30)]  # dataframe筛选指定的数据
df_new
array = np.array(df_new)  # df转为array
list = array.tolist()     # array转为list
for i in list:
    print("时间:{} 车牌:{} 北纬:{:.10f} 东经:{}".format(i[0],i[1],i[2],i[3]),end='')
    

其结果都为

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/783358.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号