导入CSV文件并执行算术运算,而无需在PYTHON中导入任何库 [英] Import CSV File and Doing arithmetic Opertions without importing any Library in PYTHON

查看:39
本文介绍了导入CSV文件并执行算术运算,而无需在PYTHON中导入任何库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的CSV文件看起来像这样

My CSV file Looks Like this

Time_stamp; Mobile_number; Download; Upload; Connection_start_time; Connection_end_time; location
1/2/2020 10:43:55;+917777777777;213455;2343;1/2/2020 10:43:55;1/2/2020 10:47:25;09443
1/3/2020 10:33:10;+919999999999;345656;3568;1/3/2020 10:33:10;1/3/2020 10:37:20;89442
1/4/2020 11:47:57;+919123456654;345789;7651;1/4/2020 11:11:10;1/4/2020 11:40:22;19441
1/5/2020 11:47:57;+919123456543;342467;4157;1/5/2020 11:44:10;1/5/2020 11:59:22;29856
1/6/2020 10:47:57;+917777777777;213455;2343;1/6/2020 10:43:55;1/6/2020 10:47:25;09443

我的问题是无需导入任何库文件我如何读取CSV文件和用户必须输入手机号码和程序应显示该编号的数据使用情况.即:算术运算(添加上行链路和下行链路)和获取该特定手机号码的结果(已使用的总数据).

MY Question is Without importing any Library file How i can read a CSV file & user have to enter the Mobile number & Program should show the Data usage of that number. ie: Arithmetic Operation (Adding Uplink & downlink ) & get the result (Total Data Used)of that specific Mobile number.

这是我的代码的样子.(我不想导入任何熊猫库.)

Here is what my code looks Like. ( i don't want to import any Pandas Library. )

import pandas as pd
df = pd.read_csv('test.csv', sep=';')
df.columns = [col.strip() for col in df.columns]
usage = df[['Download', 'Upload']][df.Mobile_number == +917777777777].sum().sum()
print(usage)

推荐答案

我将使用csv.DictReader

I'd use csv.DictReader

In [30]: with open('x', 'r') as f:
    ...:     r = csv.DictReader(f, delimiter=';')
    ...:     dct = {}
    ...:     for row in r:
    ...:         dct.setdefault(row[' Mobile_number'], []).append(row)
    ...:

In [31]: dct
Out[31]:
{'+917777777777': [OrderedDict([('Time_stamp', '1/2/2020 10:43:55'),
               (' Mobile_number', '+917777777777'),
               (' Download', '213455'),
               (' Upload', '2343'),
               (' Connection_start_time', '1/2/2020 10:43:55'),
               (' Connection_end_time', '1/2/2020 10:47:25'),
               (' location', '09443')]),
  OrderedDict([('Time_stamp', '1/6/2020 10:47:57'),
               (' Mobile_number', '+917777777777'),
               (' Download', '213455'),
               (' Upload', '2343'),
               (' Connection_start_time', '1/6/2020 10:43:55'),
               (' Connection_end_time', '1/6/2020 10:47:25'),
               (' location', '09443')])],
 '+919999999999': [OrderedDict([('Time_stamp', '1/3/2020 10:33:10'),
               (' Mobile_number', '+919999999999'),
               (' Download', '345656'),
               (' Upload', '3568'),
               (' Connection_start_time', '1/3/2020 10:33:10'),
               (' Connection_end_time', '1/3/2020 10:37:20'),
               (' location', '89442')])],
 '+919123456654': [OrderedDict([('Time_stamp', '1/4/2020 11:47:57'),
               (' Mobile_number', '+919123456654'),
               (' Download', '345789'),
               (' Upload', '7651'),
               (' Connection_start_time', '1/4/2020 11:11:10'),
               (' Connection_end_time', '1/4/2020 11:40:22'),
               (' location', '19441')])],
 '+919123456543': [OrderedDict([('Time_stamp', '1/5/2020 11:47:57'),
               (' Mobile_number', '+919123456543'),
               (' Download', '342467'),
               (' Upload', '4157'),
               (' Connection_start_time', '1/5/2020 11:44:10'),
               (' Connection_end_time', '1/5/2020 11:59:22'),
               (' location', '29856')])]}

In [32]:

然后,通过类似 usage = sum(float(_ ['Download'])+ float(_ ['Upload'])的dct ['+91777777777'])

这篇关于导入CSV文件并执行算术运算,而无需在PYTHON中导入任何库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆