python怎么读取csv文件-使用Python读写csv文件的三种方法

行之间无空行十分重要,如果有空行或者数据集中行末有空格,读取数据时一般会出错,引发[list index out of range]错误。PS:已经被这个错误坑过很多次!

使用python I/O写入和读取CSV文件

使用PythonI/O写入csv文件

以下是将"birthweight.dat"低出生体重的dat文件从作者源处下载下来,并且将其处理后保存到csv文件中的代码。

import csv

import os

import numpy as np

import random

import requests

# name of data file

# 数据集名称

birth_weight_file = 'birth_weight.csv'

# download data and create data file if file does not exist in current directory

# 如果当前文件夹下没有birth_weight.csv数据集则下载dat文件并生成csv文件

if not os.path.exists(birth_weight_file):

birthdata_url = 'https://github.com/nfmcclure/tensorflow_cookbook/raw/master/01_Introduction/07_Working_with_Data_Sources/birthweight_data/birthweight.dat'

birth_file = requests.get(birthdata_url)

birth_data = birth_file.text.split(' ')

# split分割函数,以一行作为分割函数࿰

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值