[Python] Read and Parse Files in Python
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的更多相关文章
- 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 ...
- Working with Excel Files in Python
Working with Excel Files in Python from: http://www.python-excel.org/ This site contains pointers to ...
- python 学习第五天,python模块
一,Python的模块导入 1,在写python的模块导入之前,先来讲一些Python中的概念性的问题 (1)模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质是.py ...
- Python黑帽编程1.3 Python运行时与包管理工具
Python黑帽编程1.3 Python运行时与包管理工具 0.1 本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Attack and ...
- 精通 Oracle+Python,第 6 部分:Python 支持 XML
无可辩驳的是,XML 现在是软件中信息交换的实际标准. 因此,Oracle 数据库附带了各种与 XML 相关的增强和工具,它们统称为 Oracle XML DB.XML DB 包含一系列嵌入到数据库中 ...
- Python Tutorial 学习(二)--Using the Python Interpreter
Using the Python Interpreter 2.1. Invoking the Interpreter The Python interpreter is usually install ...
- 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 ...
- 【转】利用Boost.Python将C++代码封装为Python模块
用Boost.Python将C++代码封装为Python模块 一. 基础篇 借助Boost.Python库可以将C/C++代码方便.快捷地移植到python模块当中,实现对python模块的扩 ...
- python基础系列教程——Python的安装与测试:python的IDE工具PyDev和pycharm,anaconda
---恢复内容开始--- python基础系列教程——Python的安装与测试:python的IDE工具PyDev和pycharm,anaconda 从头开启python的开发环境搭建.安装比较简单, ...
随机推荐
- bzoj1003: [ZJOI2006]物流运输(DP+spfa)
1003: [ZJOI2006]物流运输 题目:传送门 题解: 可以用spfa处理出第i天到第j都走这条路的花费,记录为cost f[i]表示前i天的最小花费:f[i]=min(f[i],f[j-1] ...
- Insert Data with C# Driver
https://docs.mongodb.com/getting-started/csharp/insert/ OverView You can use the InsertOneAsync meth ...
- NOIP2017 小凯的疑惑 解题报告(赛瓦维斯特定理)
题目描述 小凯手中有两种面值的金币,两种面值均为正整数且彼此互素.每种金币小凯都有 无数个.在不找零的情况下,仅凭这两种金币,有些物品他是无法准确支付的.现在小 凯想知道在无法准确支付的物品中,最贵的 ...
- nginx 代理https后,应用redirect https变成http --转
原文地址:http://blog.sina.com.cn/s/blog_56d8ea900101hlhv.html 情况说明nginx配置https,tomcat正常http接受nginx转发.ngi ...
- PostgreSQL Replication之第七章 理解Linux高可用(2)
7.2 衡量可用性 可用性是提供商试图保证一定的可用性级别和客户可以期望的可用性或更多.在某些情况下(取决于服务合同) 收取罚款或减少申购费用是意外停机的原因. 可用性的质量使用百分数来衡量:例如,9 ...
- PostgreSQL Replication之第四章 设置异步复制(2)
4.2 配置级联复制 正如您在本章已经看到的,设置流复制真的很容易.只需要设置几个参数,做一个基础备份,并享受您的复制设置. 在许多情况下,这种情况更有一点点微妙.在这个例子中我们假设:我们要使用一个 ...
- vue1.0父子、兄弟间 通信案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- sql中--行处理数据的两种方式
--创建待使用的表格CREATE TABLE Orders ( OrderID INT , CostValue DECIMAL(18, 2) );WITH cte_temp AS ( SELECT 1 ...
- Ubuntu(kali)开启mysql远程连接
Linux 默认关闭mysql的远程连接,编辑 /etc/mysql/my.cnf 文件, 把里面的 bind-address = 127.0.0.1 改成 bind-address = 0.0.0. ...
- 学习参考《高性能MySQL(第3版)》中文PDF+英文PDF
学习mysql数据库时推荐看看mysql 领域的经典之作<高性能mysql(第3版)>,共分为16 章和6 个附录,内容涵盖mysql 架构和历史,基准测试和性能剖析,数据库软硬件性能优化 ...