GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 文件的输入输出和pickle数据腌制 · kudou/Python@cf0e90a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • HeadFirstPython/chapter3
    • sketch.py
    • sketch1.py
    • sketch10.py
    • sketch2.py
    • sketch3.py
    • sketch4.py
    • sketch5.py
    • sketch6.py
    • sketch7.py
    • sketch8.py
    • sketch9.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎HeadFirstPython/chapter3/sketch.py‎

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
if os.path.exists('sketch.txt'):
3+
data = open('sketch.txt')
4+
for each_line in data:
5+
if not each_line.find(':') == -1:
6+
(role, line_spoken) = each_line.split(':', 1)
7+
print(role, end='')
8+
print(' said: ', end='')
9+
print(line_spoken, end='')
10+
else:
11+
print(each_line, end='')
12+
13+
data.close()
14+
else:
15+
print('The data file is missing!')
Collapse file

‎HeadFirstPython/chapter3/sketch1.py‎

Copy file name to clipboard
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
try:
2+
data = open('sketch.txt')
3+
for each_line in data:
4+
try:
5+
(role, line_spoken) = each_line.split(':', 1)
6+
print(role, end='')
7+
print(' said: ', end='')
8+
print(line_spoken, end='')
9+
except ValueError:
10+
pass
11+
data.close()
12+
except IOError:
13+
print('The data file is missing!')
Collapse file

‎HeadFirstPython/chapter3/sketch10.py‎

Copy file name to clipboard
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pickle
2+
import nester
3+
4+
new_man = []
5+
new_woman = []
6+
7+
try:
8+
with open('man_data.txt', 'rb') as man_file, open('woman_data.txt', 'rb') as woman_file:
9+
new_man = pickle.load(man_file)
10+
new_woman = pickle.load(woman_file)
11+
nester.print_lol(new_man)
12+
nester.print_lol(new_woman)
13+
print(new_man[0])
14+
print(new_woman[-1])
15+
except IOError as err:
16+
print("File error: " + str(err))
17+
18+
except pickle.PickleError as err:
19+
print("Pickle error: " + str(err))
Collapse file

‎HeadFirstPython/chapter3/sketch2.py‎

Copy file name to clipboard
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
man = []
2+
woman = []
3+
4+
try:
5+
data = open('sketch.txt')
6+
for each_line in data:
7+
try:
8+
(role, line_spoken) = each_line.split(':', 1)
9+
line_spoken = line_spoken.strip()
10+
if role == 'man':
11+
man.append(line_spoken)
12+
elif role == 'woman':
13+
woman.append(line_spoken)
14+
except ValueError:
15+
pass
16+
data.close()
17+
except IOError:
18+
print('The data file is missing!')
19+
20+
print(man)
21+
print(woman)
Collapse file

‎HeadFirstPython/chapter3/sketch3.py‎

Copy file name to clipboard
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
out = open('data', "w+")
2+
#print("Norwegian Blues stun easily.", file=out, end='')
3+
print("Norwegian Blues stun easily.", file=out)
4+
out.flush()
5+
out.close()
Collapse file

‎HeadFirstPython/chapter3/sketch4.py‎

Copy file name to clipboard
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
man = []
2+
woman = []
3+
4+
try:
5+
data = open('sketch.txt')
6+
for each_line in data:
7+
try:
8+
(role, line_spoken) = each_line.split(':', 1)
9+
line_spoken = line_spoken.strip()
10+
if role == 'man':
11+
man.append(line_spoken)
12+
elif role == 'woman':
13+
woman.append(line_spoken)
14+
except ValueError:
15+
pass
16+
data.close()
17+
except IOError:
18+
print('The data file is missing!')
19+
20+
21+
try:
22+
man_file = open("man_data.txt", 'w');
23+
woman_file = open("woman_data.txt", 'w');
24+
25+
print(man, file=man_file)
26+
print(woman, file=woman_file)
27+
28+
# man_file.close()
29+
# woman_file.close()
30+
except IOError:
31+
print('File Error')
32+
finally:
33+
man_file.close()
34+
woman_file.close()
Collapse file

‎HeadFirstPython/chapter3/sketch5.py‎

Copy file name to clipboard
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
try:
2+
data = open('missing.txt')
3+
print(data.readline(), end='')
4+
except IOError as err:
5+
print('File error: ' + str(err))
6+
finally:
7+
if 'data' in locals():
8+
data.close()
9+
10+
#print(locals())
Collapse file

‎HeadFirstPython/chapter3/sketch6.py‎

Copy file name to clipboard
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
try:
2+
with open('missing.txt') as data:
3+
print("It's...", file=data)
4+
except IOError as err:
5+
print('File error: ' + str(err))
Collapse file

‎HeadFirstPython/chapter3/sketch7.py‎

Copy file name to clipboard
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import nester
2+
man = []
3+
woman = []
4+
5+
try:
6+
data = open('sketch.txt')
7+
for each_line in data:
8+
try:
9+
(role, line_spoken) = each_line.split(':', 1)
10+
line_spoken = line_spoken.strip()
11+
if role == 'man':
12+
man.append(line_spoken)
13+
elif role == 'woman':
14+
woman.append(line_spoken)
15+
except ValueError:
16+
pass
17+
data.close()
18+
except IOError:
19+
print('The data file is missing!')
20+
21+
try:
22+
# with open('man_data.txt', 'w') as man_file:
23+
# print(man, file=man_file)
24+
# with open('woman_data.txt', 'w') as woman_file:
25+
# print(woman, file=woman_file)
26+
with open('man_data.txt', 'w') as man_file, open('woman_data.txt', 'w') as woman_file:
27+
nester.print_lol(man, fn=man_file)
28+
nester.print_lol(woman, fn=woman_file)
29+
except IOError as err:
30+
print('File error: ' + str(err))
Collapse file

‎HeadFirstPython/chapter3/sketch8.py‎

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pickle
2+
3+
with open('mydata.pickle', 'wb') as mysavedata:
4+
pickle.dump([1, 2, 'three'], mysavedata)
5+
with open('mydata.pickle', 'rb') as myrestoredata:
6+
a_list = pickle.load(myrestoredata)
7+
8+
print(a_list)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.