如何读取csv文件并根据用户输入求和值?

时间:2020-05-08 00:31:42

标签: python

  • 读取CSV文件
  • 用户必须输入手机号码
  • 程序应显示数据使用情况(即算术运算添加上行链路和下行链路)以获取结果(已使用的总数据)

这是CSV文件的示例

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

2 个答案:

答案 0 :(得分:1)

使用pandas

import pandas as pd

# read in data
df = pd.read_csv('test.csv', sep=';')

# if there are really spaces at the beginning of the column names, they should be removed
df.columns = [col.strip() for col in df.columns]

# sum Download & Upload for all occurrences of the given number
usage = df[['Download', 'Upload']][df.Mobile_number == 7777777].sum().sum()

print(usage)

>>> 431596
  • 如果要单独下载和上传
# only 1 sum()
usage = df[['Download', 'Upload']][df.Mobile_number == 7777777].sum()

print(usage)

Download    426910
Upload        4686

通过用户输入

  • 这假定Mobile_number列已作为int读入数据框
  • inputstr,因此必须将其转换为int以匹配数据框中的类型
  • df.Mobile_number == 7777777不是df.Mobile_number == '7777777'
number = int(input('Please input a phone number (numbers only)'))

usage = df[['Download', 'Upload']][df.Mobile_number == number].sum().sum()

没有导入的模块

# read file and create dict of phone numbers
phone_dict = dict()
with open('test.csv') as f:
    for i, l in enumerate(f.readlines()):
        l = l.strip().split(';')
        if (i != 0):
            mobile = l[1]
            download = int(l[2])
            upload = int(l[3])
            if phone_dict.get(mobile) == None:
                phone_dict[mobile] = {'download': [download], 'upload': [upload]}
            else:
                phone_dict[mobile]['download'].append(download)
                phone_dict[mobile]['upload'].append(upload)


print(phone_dict)
{'+917777777777': {'download': [213455, 213455], 'upload': [2343, 2343]},
 '+919999999999': {'download': [345656], 'upload': [3568]},
 '+919123456654': {'download': [345789], 'upload': [7651]},
 '+919123456543': {'download': [342467], 'upload': [4157]}}

# function to return usage
def return_usage(data: dict, number: str):
    download_usage = sum(data[number]['download'])
    upload_usage = sum(data[number]['upload'])

    return download_usage + upload_usage

# get user input to return usage
number = input('Please input a phone number')

usage = return_usage(phone_dict, number)
print(usage)
>>> Please input a phone number (numbers only) +917777777777
>>> 431596

答案 1 :(得分:0)

csv可读性不强,但是您可以看一下他的库https://pandas.pydata.org/

安装后,您可以使用:

# ask for the mobile number here
mobile_number = input('phone number? ')

df = pandas.read_csv('data.csv')

# here you will get the data for that user phone
user_data = df[df['Mobile_number'] == mobile_number].copy()

# not pretty sure in this step
user_data['download'].sum()

相关问题