This lesson will teach you how to read the contents of an external file from Python. You will also learn how to use the python csv module to read and parse csv data, as well as the json module to read and parse json data.

# f = open('animals.csv')

# for line in f:
# print(line) # f.close() # with open('animals.csv', 'r') as f:
# print(f.read()) # import csv
# with open('animals.csv', 'r') as f:
# animals = csv.reader(f)
# for row in animals:
# if row[-1] == 'True':
# print("{0} is a {1} and is housebroken".format(row[0], row[1]))
# elif row[-1] == 'False':
# print("{0} is a {1} and is not housebroken!".format(row[0], row[1])) # import json
# with open('animals.json', 'r') as r:
# data = json.load(r)
# for row in data:
# print(row['name']) # f = open('cars.txt', 'a')
# cars = ['chevy', 'tesla', 'ford']
# for car in cars:
# f.write(car + '\n') # f.close() # with open('cars.txt', 'a') as f:
# cars = ['chevy', 'vw', 'mazda']
# for car in cars:
# f.write(car + '\n') cars = [
{"make": "chevy"},
{"make": "tesla"},
{"make": "porsche"}
]
import json
with open('cars.json', 'w') as f:
json.dump(cars, f)

[Python] Read and Parse Files in Python的更多相关文章

  1. Huge CSV and XML Files in Python, Error: field larger than field limit (131072)

    Huge CSV and XML Files in Python January 22, 2009. Filed under python twitter facebook pinterest lin ...

  2. Working with Excel Files in Python

    Working with Excel Files in Python from: http://www.python-excel.org/ This site contains pointers to ...

  3. python 学习第五天,python模块

    一,Python的模块导入 1,在写python的模块导入之前,先来讲一些Python中的概念性的问题 (1)模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质是.py ...

  4. Python黑帽编程1.3 Python运行时与包管理工具

    Python黑帽编程1.3  Python运行时与包管理工具 0.1  本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Attack and ...

  5. 精通 Oracle+Python,第 6 部分:Python 支持 XML

    无可辩驳的是,XML 现在是软件中信息交换的实际标准. 因此,Oracle 数据库附带了各种与 XML 相关的增强和工具,它们统称为 Oracle XML DB.XML DB 包含一系列嵌入到数据库中 ...

  6. Python Tutorial 学习(二)--Using the Python Interpreter

    Using the Python Interpreter 2.1. Invoking the Interpreter The Python interpreter is usually install ...

  7. Error: Can't find Python executable, you can set the PYTHON env variable.

    该错误解决方案. NodeJS安装Npm包时出现错误: npm WARN prefer global node-gyp@3.4.0 should be installed with -g > s ...

  8. 【转】利用Boost.Python将C++代码封装为Python模块

    用Boost.Python将C++代码封装为Python模块 一.     基础篇 借助Boost.Python库可以将C/C++代码方便.快捷地移植到python模块当中,实现对python模块的扩 ...

  9. python基础系列教程——Python的安装与测试:python的IDE工具PyDev和pycharm,anaconda

    ---恢复内容开始--- python基础系列教程——Python的安装与测试:python的IDE工具PyDev和pycharm,anaconda 从头开启python的开发环境搭建.安装比较简单, ...

随机推荐

  1. Find or Query Data with the mongo Shell

    https://docs.mongodb.com/getting-started/shell/query/ Overview You can use the find() method to issu ...

  2. github README.md教程

    github README.md教程 总结 github中README.md通过特殊字符标记和缩进来达到格式控制,也可以用HTML标签来实现格式控制. 教程一: Markdown 的目标是实现「易读易 ...

  3. springboot actuator shutdown正确的关闭操作

    今天整合ehcache时发现一个很重要的问题,就是程序关闭(硬关闭)之后,持久化到磁盘的缓存数据没能正确写入加载,问题还是硬关闭的问题,所以就使用actuator 进行监听 <dependenc ...

  4. Spring深入浅出(一)IOC的基本知识

    Spring前言 Spring是一个企业级开发框架,为解决企业级项目开发过于复杂而创建的,框架的主要优势之一就是分层架构,允许开发者自主选择组件. Spring的两大核心机制是IOC(控制反转)和AO ...

  5. (VC)搭建OpenGL编程环境

    1.下载glut工具包 opengl需要用到的库.下载glut: http://pan.baidu.com/s/1i4c8sHf 2.安装glut a)解压上面下载到的glut工具包后会得到5个文件, ...

  6. 如何在一个 U 盘上安装多个 Linux 发行版

    作者: Ambarish Kumar 译者: LCTT geekpi 本教程介绍如何在一个 U 盘上安装多个 Linux 发行版.这样,你可以在单个 U 盘上享受多个现场版live Linux 发行版 ...

  7. Mac配置PHP环境

    本文章来自:http://blog.csdn.net/wj_november/article/details/51417491 本人使用的是:MacOs 10.12.3,根据如上操作已经安装成功,感谢 ...

  8. Highcharts 前端图表插件

    Highcharts 支持将图片下载成各种格式 Highcharts官网:https://www.hcharts.cn/download <div id="container" ...

  9. Hadoop集群配置搭建

    环境:Centos 6.9,Hadoop 2.7.1,JDK 1.8.0_161,Maven 3.3.9 前言: 1.配置一台master服务器,两台或多台slave服务器.    2.master可 ...

  10. iOS开发——捕获崩溃信息

    可通过注册NSUncaughtExceptionHandler捕获异常信息,将拿到的NSException细节写入Crash日志,精准的定位出错程序位置,有需要的,可直接将crash信息直接上传服务器 ...