import csv

headers = ["class", "name", "sex", "height", "year"]
# rows = [
# [1, 'xiaoming', 'male', 168, 23],
# [1, 'xiaohong', 'female', 162, 22],
# [2, 'xiaozhang', 'female', 163, 21],
# [2, 'xiaoli', 'male', 158, 21]
# ]
# with open("test.csv", "w", newline="") as f:
# f_csv = csv.writer(f)
# f_csv.writerow(headers)
# f_csv.writerows(rows) # rows = [
# {'class': 1, 'name': 'xiaoming', 'sex': 'male', 'height': 168, 'year': 23},
# {'class': 1, 'name': 'xiaohong', 'sex': 'female', 'height': 162, 'year': 22},
# {'class': 2, 'name': 'xiaozhang', 'sex': 'female', 'height': 163, 'year': 21},
# {'class': 2, 'name': 'xiaoli', 'sex': 'male', 'height': 158, 'year': 21},
# ]
# with open("test2.csv", "w", newline="") as f:
# f_csv = csv.DictWriter(f, headers)
# f_csv.writeheader()
# f_csv.writerows(rows) # 列表读取
with open("test2.csv") as f:
f_csv = csv.reader(f)
for row in f_csv:
print(row) # 字典读取
with open("test2.csv") as f:
f_csv = csv.DictReader(f)
for row in f_csv:
print(row["sex"])

12.python csv文件写入和读出的更多相关文章

  1. python CSV 文件的读写

    1.CSV文件 import csv with open(r"E:\code\0_DataSet\tianchi_2015_mobile_recommand\fresh_comp_offli ...

  2. python csv文件转换成xml, 构建新xml文件

    csv文件 code from xml.etree.ElementTree import Element,ElementTree,tostring import json,csv def csvtox ...

  3. python csv文件打开错误:_csv.Error: line contains NULL byte

    当python读取文件出现_csv.Error: line contains NULL byte时, # -*- coding:utf-8 -*- import csv with open(r'E:\ ...

  4. Python csv文件操作

    一.open文件打开和with open as 文件打开的区别 file= open("test.txt","r") try: for line in file ...

  5. 将CSV文件写入MySQL

    先打开CSV文件查看第一行有哪些字段,然后新建数据库,新建表.(若字段内容很多建议类型text,如果设成char后续会报错) 命令如下: load data infile '路径XXXX.csv' i ...

  6. ofstream的使用方法--超级精细。C++文件写入、读出函数(转)

    ofstream的使用方法ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间;  在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的, ...

  7. C++文件写入,读出函数ofstream,ifstream的使用方法

    ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间. 1.插入器(<<)  向流输出数据.比如说系统有一个默认的标准输出流(cout),一般情况下 ...

  8. python 使用csv 文件写入 出现多余空行数据解决方案

    因为csv.writerow() 方法会造成读取时每条数据后多一条空数据 解决方案如下: 分为两种情况 python2 和 python3 先说python2版本 with open('xxx.csv ...

  9. java文件写入和读出的序列化

    文件的写入入与读出都有它们自己的格式,不便于读入和取出,implement Serializable接口,实现任何个事文件的写入和读取取:

随机推荐

  1. sklearn.preprocessing.StandardScaler数据标准化

    原文链接:https://blog.csdn.net/weixin_39175124/article/details/79463993 数据在前处理的时候,经常会涉及到数据标准化.将现有的数据通过某种 ...

  2. 【Linux 网络编程】网络IP地址结构体

    (1)IPv4套接口地址结构通常也称为"网际套接字地址结构",它以"sockaddr_in"命名,        定义在<netinet/in.h> ...

  3. 使用注解@CrossOrigin解决跨域问题

    转一个大兄弟写的贴子,总结得很好,很全面 https://www.cnblogs.com/mmzs/p/9167743.html 作者: 淼淼之森

  4. Zookeeper - zookeeper安装与配置

    1.什么时Zookeeper ZooKeeper:分布式服务框架 Zookeeper -- 管理分布式环境中的数据. 2.安装 1>官网下载压缩包并解压zookeeper-3.4.14.zip ...

  5. Javascript设计原则

    Javascript设计原则 在面向对象的程序设计思想中, 我们能够遵循一些原则能够让我们开发代码时结构层次清晰, 更具说服力, 可谓是事半功倍. 做到这一点我们掌握一些程序设计原则是非常有利的, 如 ...

  6. linux 内核数据结构之红黑树.

    转载: http://www.cnblogs.com/haippy/archive/2012/09/02/2668099.html https://zh.wikipedia.org/zh/%E7%BA ...

  7. python_操作MySQL 初解

    单文件操作数据库 import random import threading, multiprocessing import time, datetime import pymysql import ...

  8. 搜索专题:Balloons

    搜索专题:Balloons 这道题一看与时间有关,第一想到的就是BFS,定义一个状态,包含每一个状态的剩余气球数,已经进行的时间和每一个志愿者上一次吹气球的时间: 每一次状态转换时,检查是否有没有使用 ...

  9. js汉字转换为拼音

    片段 1 片段 2 gistfile1.txt /* --- description: Pinyin, to get chinese pinyin from chinese. license: MIT ...

  10. leetcode 75. Sort Colors (荷兰三色旗问题)

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...