书画网站 建站,广西建设网郭业棚,个人的网站怎么备案表,wordpress防采集源码CSV#xff1a;简化的电子表格#xff0c;被保存为纯文本文件
JSON#xff1a;是一种数据交换格式#xff0c;易于人阅读和编写#xff0c;同时也易于机器解析和生成#xff0c;以JavaScript源代码的形式将信息保存在纯文本文件中
一、csv模块
CSV文件中的每行代表电…CSV简化的电子表格被保存为纯文本文件
JSON是一种数据交换格式易于人阅读和编写同时也易于机器解析和生成以JavaScript源代码的形式将信息保存在纯文本文件中
一、csv模块
CSV文件中的每行代表电子表格中的一行逗号分隔了该行中的单元格。但并非CSV文件中的每个逗号都表示两个单元格之间的分界。CSV文件也有自己的转义字符允许逗号和其他字符作为值的一部分。所以总是应该使用csv模块来读写CSV文件。
1、reader对象
reader对象让你迭代遍历CSV文件中的每一行 使用Python的csv模块读取CSV文件可以归结为以下几个步骤 导入csv模块 首先你需要导入Python的csv模块这样你才能使用它提供的功能。 打开CSV文件 使用open()函数打开CSV文件。你需要提供文件路径作为参数并指定模式通常是r表示只读。 创建CSV读取器 将open()函数返回的文件对象传递给csv.reader()函数这将创建一个CSV读取器对象。 读取数据 使用list()函数将CSV读取器对象转换成一个普通的Python列表。这个列表将包含文件中的每一行其中每一行都是一个列表包含该行中的所有字段。 访问数据 可以用表达式exampleData[row] [col]来访问特定行和列的值。 import csv
exampleFile open(example.csv)
exampleReader csv.reader(exampleFile)
exampleData list(exampleReader)
print(exampleData) [[4/5/2015 13:34, Apples, 73], [4/5/2015 3:41, Cherries, 85], [4/6/2015 12:46, Pears, 14], [4/8/2015 8:59, Oranges, 52]] 2、在for循环中从reader对象读取数据
对于大型的CSV文件你需要在一个for循环中使用reader对象。这样可以避免将整个文件一次性装入内存。
import csv
exampleFile open(example.csv)
exampleReader csv.reader(exampleFile)
for row in exampleReader: #line_num变量为当前行的编号print(Row # str(exampleReader.line_num) str(row)) 3、writer对象
writer对象让你将数据写入 CSV 文件。
import csv
exampleFile open(example.csv,w,newline)
exampleWriter csv.writer(exampleFile)
exampleWriter.writerow([1,2,3,45,5])
exampleWriter.writerow([Hello, eggs, bacon, ham])
exampleWriter.writerow([Hello,, eggs,, bacon,, ham,])
在Windows操作系统上需要为open()函数的newline关键字参数传入一个空字符串保证文件单倍行距
在CSV文件中writer对象使用双引号自动转义了Hello, world!中的逗号 4、delimiter和lineterminator关键字参数
delimiter分隔符。默认情况下CSV文件的分隔符是逗号。
lineterminator行终止字符。默认情况下行终止字符是换行符。
例如希望用制表符代替逗号来分隔单元格并希望有两倍行距
import csv
exampleFile open(example.csv,w,newline)
exampleWriter csv.writer(exampleFile,delimiter\t, lineterminator\n\n)
exampleWriter.writerow([1,2,3,45,5])
exampleWriter.writerow([Hello, eggs, bacon, ham])
exampleWriter.writerow([Hello,, eggs,, bacon,, ham,]) 5、DictReader和DictWriter CSV对象
reader和writer对象通过使用列表对CSV文件的行进行读写。DictReader和DictWriter CSV对象实现相同的功能但使用的是字典且它们使用CSV文件的第一行作为这些字典的键。
1DictReader
例子第一行列标题为NamePet和Phone import csv
exampleFile open(example.csv)
exampleReader csv.DictReader(exampleFile)
for row in exampleReader:print(row[Name], row[Pet], row[Phone])
如果csv文件中第一行不是列标题想使用键值对形式读取文件时在DictReader()中添加一个参数即可
exampleDictReader csv.DictReader(exampleFile, [Name, Pet, Phone]) 2DictWriter
import csv
exampleFile open(example.csv,w,newline)
exampleDictWriter csv.DictWriter(exampleFile,fieldnames[Name,Pet,Phone])
exampleDictWriter.writeheader()
exampleDictWriter.writerow({Name: Alice, Pet: cat, Phone: 555- 1234})
exampleDictWriter.writerow({Name: Bob, Phone: 555-9999})
exampleDictWriter.writerow({Phone: 555-5555, Name: Carol, Pet: dog})
writeheader() 方法决定包含标题行
传入writerow()的字典中的键-值对的顺序并不重要它们是按照给DictWriter()的键的顺序写的。
二、JSON和API
JSON是一种轻量级的数据交换格式易于人阅读和编写同时也易于机器解析和生成。
许多网站应用API允许开发者请求数据并JSON格式返回数据使得开发者可以轻松地进行数据交换和网络编程。
例子JSON文件格式
{name: John Doe,age: 30,isEmployed: true,address: {street: 123 Main St,city: Anytown,state: CA,zip: 12345},phoneNumbers: [{type: home,number: 555-1234},{type: office,number: 555-5678}],children: null,hobbies: [reading, hiking, coding]
}
三、json模块
JSON不能存储每一种Python值它只能包含以下数据类型的值字符串、整型、浮点型、布尔型、列表、字典和NoneType。JSON字符串总是用双引号
1、用loads()函数读取JSON
这个名字的意思是“load string”而不是“loads”
loads()函数将该数据返回为一个Python字典
import json
stringOfJsonData {name: Zophie, isCat: true, miceCaught: 0,felineIQ: null}
jsonDataAsPythonValue json.loads(stringOfJsonData)
print(jsonDataAsPythonValue) {name: Zophie, isCat: True, miceCaught: 0, felineIQ: None}
2、用dumps函数写出JSON
json.dumps()函数表示“dump string”而不是“dumps”将一个Python值转换成JSON格式的数据字符串。
import json
pythonValue {isCat: True, miceCaught: 0, name: Zophie, felineIQ: None}
stringOfJsonData json.dumps(pythonValue)
print(stringOfJsonData) {isCat: true, miceCaught: 0, name: Zophie, felineIQ: null}