Skip to content

Commit 3bf0bfd

Browse files
committed
Add code of chapter 14
1 parent d71b927 commit 3bf0bfd

14 files changed

+151
-1
lines changed

14-csv-json/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# csv
2+
<pre>
3+
import csv
4+
csvFileObj = open(csvFilename)
5+
readerObj = csv.reader(csvFileObj)import csv
6+
</pre>
7+
8+
# json
9+
<pre>
10+
import json
11+
json.loads()
12+
json.dumps()
13+
</pre>

14-csv-json/delimiterCsv.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import csv
2+
3+
csvFile = open('example.tsv', 'w', newline='')
4+
csvWriter = csv.writer(csvFile, delimiter='\t', lineterminator='\n\n')
5+
csvWriter.writerow(['apples', 'oranges', 'grapes'])
6+
csvWriter.writerow(['eggs', 'bacon', 'ham'])
7+
csvWriter.writerow(['spam', 'spam', 'spam', 'spam', 'spam', 'spam'])
8+
9+
csvFile.close()

14-csv-json/example.csv

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
4/5/2014 13:34,Apples,73
2+
4/5/2014 3:41,Cherries,85
3+
4/6/2014 12:46,Pears,14
4+
4/8/2014 8:59,Oranges,52
5+
4/10/2014 2:07,Apples,152
6+
4/10/2014 18:10,Bananas,23
7+
4/10/2014 2:40,Strawberries,98

14-csv-json/example.tsv

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apples oranges grapes
2+
3+
eggs bacon ham
4+
5+
spam spam spam spam spam spam
6+

14-csv-json/excelSpreadsheets.zip

48.6 KB
Binary file not shown.

14-csv-json/excelToCsv.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import csv
2+
import os
3+
import openpyxl
4+
5+
for excelFile in os.listdir('.'):
6+
# Skip non-xlsx files, load the workbook object
7+
for sheetName in wb.get_sheet_names():
8+
# Loop through every sheet in the workbook
9+
sheet = wb.get_sheet_by_name(sheetname)
10+
11+
# Create the CSV filename from the Excel filename and sheet title
12+
# Create the csv.writer object for CSV file.
13+
14+
# Loop through every row in the sheet.
15+
for rowNum in range(1, sheet.max_row+1):
16+
rowData = []
17+
for colNum in range(1, sheet.max_column+1):
18+
# Append each cell's data to rowData
19+
20+
# Write the rowData list to the CSV file.
21+
22+
csvFile.close()

14-csv-json/jsonLoadsAndDumps.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import json
2+
3+
stringOfJsonData = '{"name": "Zophie", "isCat": true, "miceCaught": 0, "felineIQ": null}'
4+
jsonDataAsPythonValue = json.loads(stringOfJsonData)
5+
print(jsonDataAsPythonValue)
6+
print(json.dumps(jsonDataAsPythonValue))
7+

14-csv-json/output.csv

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spam,eggs,bacon,ham
2+
"Hello, world!",eggs,bacon,ham
3+
1,2,3.141592,4

14-csv-json/quickWeather.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#! python3
2+
# quickWeather.py - Prints the current weather for a location from the command line.
3+
4+
import json
5+
import sys
6+
import requests
7+
8+
# Compute location from command line arguments.
9+
if len(sys.argv) < 2:
10+
print('Usage: quickWeather.py location')
11+
sys.exit()
12+
location = ' '.join(sys.argv[1:])
13+
14+
# Download the JSON data from OpenWeatherMap.org's API
15+
url ='http://api.openweathermap.org/data/2.5/forecast/daily?q=%s&cnt=3' % (location)
16+
response = requests.get(url)
17+
response.raise_for_status()
18+
19+
# Load JSON data into a Python variable.
20+
weatherData = json.loads(response.text)
21+
22+
# Print weather descriptions.
23+
w = weatherData['list']
24+
print('Current weather in %s:' % (location))
25+
print(w[0]['weather'][0]['main'], '-', w[0]['weather'][0]['description'])
26+
print()
27+
print('Tomorrow:')
28+
print(w[1]['weather'][0]['main'], '-', w[1]['weather'][0]['description'])
29+
print()
30+
print('Day after tomorrow:')
31+
print(w[2]['weather'][0]['main'], '-', w[2]['weather'][0]['description'])

14-csv-json/readCsv.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import csv
2+
3+
exampleFile = open('example.csv')
4+
exampleReader = csv.reader(exampleFile)
5+
6+
for row in exampleReader:
7+
print(row)
8+
9+
exampleFile.close()

14-csv-json/removeCsvHeader.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#! python3
2+
# removeCsvHeader.py - Removes the header from all CSV files in the current
3+
# working directory.
4+
5+
import csv
6+
import os
7+
8+
os.makedirs('headerRemoved', exist_ok=True)
9+
10+
# Loop through every file in the current working directory.
11+
for csvFilename in os.listdir('.'):
12+
if not csvFilename.endswith('.csv'):
13+
continue # skip non-csv files
14+
15+
print('Removing header from ' + csvFilename + '...')
16+
17+
# Read the CSV file in (skipping first row).
18+
csvRows = []
19+
csvFileObj = open(csvFilename)
20+
readerObj = csv.reader(csvFileObj)
21+
for row in readerObj:
22+
if readerObj.line_num == 1:
23+
continue # skip first row
24+
csvRows.append(row)
25+
csvFileObj.close()
26+
27+
# Write out the CSV file.
28+
csvFileObj = open(os.path.join('headerRemoved', csvFilename), 'w', newline='')
29+
csvWriter = csv.writer(csvFileObj)
30+
for row in csvRows:
31+
csvWriter.writerow(row)
32+
csvFileObj.close()

14-csv-json/removeCsvHeader.zip

662 KB
Binary file not shown.

14-csv-json/writeCsv.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import csv
2+
3+
outputFile = open('output.csv', 'w', newline='')
4+
outputWriter = csv.writer(outputFile)
5+
outputWriter.writerow(['spam', 'eggs', 'bacon', 'ham'])
6+
outputWriter.writerow(['Hello, world!', 'eggs', 'bacon', 'ham'])
7+
outputWriter.writerow([1, 2, 3.141592, 4])
8+
9+
outputFile.close()
10+

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# AutomatePython
2-
Python 编程快速上手——让繁琐工作自动化
2+
* Python 编程快速上手——让繁琐工作自动化
3+
* <a>http://inventwithpython.com</a>

0 commit comments

Comments
 (0)