dongpangbu4016 2015-09-07 17: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-07 17: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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部