“Comma-Separated Values(逗号分隔的值)JSON 是JavaScript Object Notation
import csv
#“Comma-Separated Values(逗号分隔的值)JSON 是JavaScript Object Notation
# 利用csv模块从CSV文件中读取数据
# open()函数打开文件,返回file对象
exampleFile = open('example.csv')
# 使用csv.reader()函数返回reader对象
exampleReader = csv.reader(exampleFile)
# 转化为python列表
exampleData = list(exampleReader)
# [['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'], ['4/10/2015 2:07', 'Apples', '152'], ['4/10/2015 18:10', 'Bananas', '23'], ['4/10/2015 2:40', 'Strawberries', '98']]
print(exampleData)
"""
Out[33]:
[['4/5/2014 13:34', 'Apples', '73'],
['4/5/2014 3:41', 'Cherries', '85'],
['4/6/2014 12:46', 'Pears', '14'],
['4/8/2014 8:59', 'Oranges', '52'],
['4/10/2014 2:07', 'Apples', '152'],
['4/10/2014 18:10', 'Bananas', '23'],
['4/10/2014 2:40', 'Strawberries', '98']]
In[34]:
exampleData[0][1]
Out[34]:
'Apples'
In[35]:
exampleData[2][1]
Out[35]:
'Pears'
"""
exampleReader = csv.reader(exampleFile)
for row in exampleReader:
print('Row #' + str(exampleReader.line_num) + ' ' + str(row))
# 将数据写入CSV文件
outputFile = open('output.csv', 'w', newline='')
outputWriter = csv.writer(outputFile, delimiter="\t", lineterminator='\n')
outputWriter.writerow(['spam', 'eggs', 'bacon', 'ham'])
outputWriter.writerow(['Hello, world!', 'eggs', 'bacon', 'ham'])
outputWriter.writerow([1, 2, 3.141592, 4])
outputFile.close()
import json
# 将json数据字符串转化为python的值(字典)
stringOfJsonData = '{"name": "Zophie", "isCat": true, "miceCaught": 0, "felineIQ": null}'
jsonDataAsPythonValue = json.loads(stringOfJsonData)
# >>> jsonDataAsPythonValue {'isCat': True, 'miceCaught': 0, 'name': 'Zophie', 'felineIQ': None}
print(jsonDataAsPythonValue)
print(type(jsonDataAsPythonValue))
stringOfJsonData = json.dumps(jsonDataAsPythonValue)
print(type(stringOfJsonData))
Python处理Excel文档之openpyxl
import openpyxl
print(33)
# 用 openpyxl 模块打开 Excel 文档
wb = openpyxl.load_workbook("example.xlsx")
print(type(wb))
#<class 'openpyxl.workbook.workbook.Workbook'>
# 从工作簿中取得工作表
print(wb.get_sheet_names())
#['Sheet1', 'Sheet2', 'Sheet3']
sheet = wb.get_sheet_by_name("Sheet3")
print(type(sheet))
#<class 'openpyxl.worksheet.worksheet.Worksheet'>
print(sheet.title)
#'Sheet3'
anotherSheet=wb.get_active_sheet()
print(anotherSheet.title)
# 从表中取得单元格
sheet = wb.get_sheet_by_name("Sheet1")
print(sheet["A1"])
print(type(sheet["A1"]))
#<class 'openpyxl.cell.cell.Cell'>
print(type(sheet["A1"].value))
#<class 'datetime.datetime'>
#datetime.datetime(2015, 4, 5, 13, 34, 2)
print(sheet["A1"].value)
c = sheet['B1']
print('Row ' + str(c.row) + ', Column ' + str(c.column) + ' is ' + c.value)
#'Row 1, Column 2 is Apples'
print(c.coordinate)
#'B1'
print(sheet.cell(row=1, column=2))
tuple(sheet["A1":"C3"])
#((< Cell 'Sheet1'.A1 >, < Cell 'Sheet1'.B1 >, < Cell 'Sheet1'.C1 >),
# (< Cell 'Sheet1'.A2 >, < Cell 'Sheet1'.B2 >, < Cell 'Sheet1'.C2 >),
#(< Cell 'Sheet1'.A3 >, < Cell 'Sheet1'.B3 >, < Cell 'Sheet1'.C3 >))
for rowOfCellObjects in sheet['A1':'C3']:
for cellObj in rowOfCellObjects:
print(cellObj.coordinate, cellObj.value)
print('--- END OF ROW ---')
网友评论