根据用户输入的匹配对csv文件的值求和(Sum the values of a csv file based on a match from user input)

编程入门 行业动态 更新时间:2024-10-26 05:17:20
根据用户输入的匹配对csv文件的值求和(Sum the values of a csv file based on a match from user input)

问题描述

我有一个csv文件,有3位信息,如下所示,用逗号分隔。 我有我的代码,如果我完全按照它写入的方式输入第一个值,我现在会返回第二个和第三个值。

示例我输入的用户如果输入为25.803.1.1确实告诉我name = BALLOON并且连接等于357.我想做这个用户输入的工作25.803.1.1它会建议连接等于357,但是如果用户进入25.803。 它会给匹配25.803的所有内容提供连接总和。 如果用户输入25.它将给出以25开头的所有内容的连接总和。

所以我假设我需要一个if语句,可以查看是否有1个小数或2个小数位或3个小数位,然后打印出结果。 我不确定是否最好进行某种分组,过滤或匹配,或者如果我需要创建一个新的列表,将十进制日期的第一位分成一个看起来像[[25]的列表, [803],[1],[1]]作为一个例子然后尝试根据用户以某种方式输入的内容对其进行索引。 非常感谢任何帮助。

当前代码,如果输入为25.803.1.1,则有效

with open("D:/Python/Data/rttData.csv") as csvfile: readCSV = csv.reader(csvfile, delimiter=',') enbIDs = [] enbNames = [] enbRRCs = [] for row in readCSV: enbID = row[0] enbName = row[1] enbRRC = row[2] enbNames.append(enbName) enbIDs.append(enbID) enbRRCs.append(enbRRC) siteSelection = input('Enter the eNB number below to search for [ex: 25.803.1.1]') enbIndex = enbIDs.index(siteSelection) theName = enbNames[enbIndex] theRRC = enbRRCs[enbIndex] print('The name of site', siteSelection, 'is:', theName,'and the RRC Connections are:',theRRC)

CSV文件

25.803.1.1,BALLOON,357 25.803.2.1,BALLOON,1941 25.803.3.1,BALLOON,3802 25.803.12.2,BALLOON,18783 25.803.22.2,BALLOON,20136 25.803.32.2,BALLOON,17560 25.803.14.4,BALLOON,2661 25.803.24.4,BALLOON,3472 25.803.34.4,BALLOON,4379 25.804.1.1,BANK,8410 25.804.2.1,BANK,7656 25.804.3.1,BANK,9822 25.804.12.2,BANK,9418 25.804.22.2,BANK,20522 25.804.32.2,BANK,14694 25.804.14.4,BANK,17569 25.804.24.4,BANK,16137 25.804.34.4,BANK,13835

Problem Description

I have a csv file that has 3 bits of information as seen below which are separated by commas. I have my code which currently returns me the 2nd and 3rd value as I'd like if I enter the first value exactly as it's written.

Example my input from the user if entered as 25.803.1.1 does tell me name = BALLOON and connections equals 357. I would like to make this work in which the user inputs 25.803.1.1 it will advise connections equals 357, but if the user enters 25.803. it would give the connections sum from everything matching 25.803. If the user entered 25. that it would give the sum of connections for everything starting with 25.

So I assume I need an if statement that could see if there were 1 decimal or 2 decimal places or 3 decimal places and then print out the result. I'm not sure if it would be best to do some sort of grouping, filtering or matching or if I need to create a new list that splits the first bit of date by decimal into a list which would look like [[25], [803], [1], [1]] for an example and then try indexing it based upon what the user inputs somehow. Any assistance is greatly appreciated.

Current Code that works if entered as 25.803.1.1

with open("D:/Python/Data/rttData.csv") as csvfile: readCSV = csv.reader(csvfile, delimiter=',') enbIDs = [] enbNames = [] enbRRCs = [] for row in readCSV: enbID = row[0] enbName = row[1] enbRRC = row[2] enbNames.append(enbName) enbIDs.append(enbID) enbRRCs.append(enbRRC) siteSelection = input('Enter the eNB number below to search for [ex: 25.803.1.1]') enbIndex = enbIDs.index(siteSelection) theName = enbNames[enbIndex] theRRC = enbRRCs[enbIndex] print('The name of site', siteSelection, 'is:', theName,'and the RRC Connections are:',theRRC)

CSV File

25.803.1.1,BALLOON,357 25.803.2.1,BALLOON,1941 25.803.3.1,BALLOON,3802 25.803.12.2,BALLOON,18783 25.803.22.2,BALLOON,20136 25.803.32.2,BALLOON,17560 25.803.14.4,BALLOON,2661 25.803.24.4,BALLOON,3472 25.803.34.4,BALLOON,4379 25.804.1.1,BANK,8410 25.804.2.1,BANK,7656 25.804.3.1,BANK,9822 25.804.12.2,BANK,9418 25.804.22.2,BANK,20522 25.804.32.2,BANK,14694 25.804.14.4,BANK,17569 25.804.24.4,BANK,16137 25.804.34.4,BANK,13835

最满意答案

您需要检查每条记录的ID,以查看它与您的查询部分匹配。 以下解决方案通过使用startswith字符串方法来实现。 可以实现更智能的部分匹配,但这应该让您走上正确的轨道。

#! /usr/bin/env python3 import collections import csv import itertools Record = collections.namedtuple('Record', 'id, name, rrc') def main(): with open('rttData.csv', newline='') as file: records = tuple(itertools.starmap(Record, csv.reader(file))) query = input('Enter the ID to search for: ') for record in filter(lambda item: item.id.startswith(query), records): print(f'ID: {record.id}\nName: {record.name}\nRRC: {record.rrc}\n') if __name__ == '__main__': main()

You need to check the ID of each record to see it there is a partial match to your query. The following solution does just that by using the startswith string method. A more intelligent partial match could be implemented, but this should get you on the right track.

#! /usr/bin/env python3 import collections import csv import itertools Record = collections.namedtuple('Record', 'id, name, rrc') def main(): with open('rttData.csv', newline='') as file: records = tuple(itertools.starmap(Record, csv.reader(file))) query = input('Enter the ID to search for: ') for record in filter(lambda item: item.id.startswith(query), records): print(f'ID: {record.id}\nName: {record.name}\nRRC: {record.rrc}\n') if __name__ == '__main__': main()

更多推荐

本文发布于:2023-07-26 03:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1270735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   用户   Sum   values   csv

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!