博客
关于我
python读取csv文件
阅读量:128 次
发布时间:2019-02-26

本文共 728 字,大约阅读时间需要 2 分钟。

Python读取CSV文件是处理批量数据导入任务常用的方法。以下是基于Python的代码实现,用于从CSV文件中读取数据。

def uploadfile(cls, file):    results = {'usr_err_mes': 0, 'data': [], 'info': []}    csv_data = []        with open(file, "r", encoding='gbk') as csvfile:        reader = csv.DictReader((line.replace('\0', '') for line in csvfile))        for line in reader:            if not line:                break            else:                csv_data.append(line)        if not csv_data:        raise ValueError(_('There is no data in csv file'))    # 以下部分尚未实现

这段代码定义了一个用于上传文件的函数,主要功能是读取CSV文件内容。如果文件中没有有效数据,会抛出异常提示。代码中使用csv.DictReader来解析CSV文件,并通过gbk编码支持中文环境。

通过Postman测试时,请注意以下几点:

  • 确保CSV文件路径正确
  • 测试请求头设置为form-data
  • 上传文件时请勿包含空白行或特殊字符
  • 如果在测试过程中遇到问题,请检查文件路径是否正确,并确保文件中没有包含不兼容的字符。

    转载地址:http://pnky.baihongyu.com/

    你可能感兴趣的文章
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    npm切换到淘宝源
    查看>>
    npm前端包管理工具简介---npm工作笔记001
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm错误Error: Cannot find module ‘postcss-loader‘
    查看>>
    NPOI之Excel——合并单元格、设置样式、输入公式
    查看>>
    NPOI利用多任务模式分批写入多个Excel
    查看>>
    NR,NF,FNR
    查看>>
    nrf开发笔记一开发软件
    查看>>
    NSDateFormatter的替代方法
    查看>>
    nsis 安装脚本示例(转)
    查看>>
    NSOperation基本操作
    查看>>
    NSRange 范围
    查看>>
    NSSet集合 无序的 不能重复的
    查看>>
    NT AUTHORITY\NETWORK SERVICE 权限问题
    查看>>