dongpangbu4016 2015-09-08 01:19
浏览 133
已采纳

读取和写入适合Excel样式列/行操作的数据结构的CSV文件

So I am currently working on a web-application with a few other people for a client, and we've hit a stumbling block. Basically we need to be able to upload a CSV file in a specific layout - and the application will take that CSV file and based on specific columns and their values, it will perform the algorithm and calculations required.

The output would also be a downloadable CSV file. None of us have had experience working with CSV in Python.

The layout of the CSV file is as follows: ID, Name, Address, Suburb, Postcode, Email, Phone

I need to take the address fields and use that in a calculation to determine how to get to the destination from their specific address. I would also need to print the specific details related to that person as well.

EDIT Okay so basically, the CSV file will contain details about employees and their relevant personal information. What our application does is takes that information, and based on the employees address, will predict the most optimised route for them to get to the destination. Basically how the hell do I read CSV files and then write an algorithm based on a certain column/row to perform my calculations required.

  • 写回答

1条回答 默认 最新

  • dpyu7978 2015-09-08 01:27
    关注

    Reading a .csv is easy with the csv standard library module.

    A more efficient library that allows for better manipulation of .csv files is pandas, you should consider playing around with this one first.

    For instance, given a csv file:

    csv = r"""col1,col2,col3,col4
              bar,20150301,homer,53
              foo,20150502,bart,102
              barfoo,20150201,lisa,13
              foobar,20150501,marge,97"""
    

    We can operate on it with the csv module:

    import csv # built-in no need to install
    from StringIO import StringIO 
    
    with open(StringIO(csv), 'rb') as f:
        reader = csv.reader(f)
        for row in reader:
            # Do whatever you need
    

    And, similarly, with pandas:

    import pandas as pnd # external, installation required
    
    # returns a dataframe, specify cols, index et cetera 
    df = pnd.read_csv(StringIO(csv),
        header=0,
        index_col=["col1", "col3"], 
        usecols=["col1", "col2", "col3"],
        parse_dates=["col2"])
    
    # do dirty things with it.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题