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 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)