Python学習3月8日【pythonプログラミング入門から実践へ】---ノート(1)

9789 ワード




#      ,               
# ,
# , python ,
# json, ,

10.1         

#10.1.1



#10.1.3
##  For  
# filename= 'pi_digits.txt'
# with open (filename) as file_object:# with, python 。
# for line in file_object:#
# print(line.rstrip())

#10.1.4                
# filename = 'pi_digits.txt'
# with open(filename) as file_object:
# lines = file_object.readlines()
# for line in lines:
# print(line.rstrip())


#10.2.1     
 
filename = 'programming.txt'

with open (filename,"w") as file_object:
file_object.write(" I love programming.")


#10.2.3

# ‘a' ,
filename = 'programming.txt'

with open (filename,"a") as file_object:
file_object.write(" also love finding meaning in large datasets.
")
file_object.write(" i love creating apps that can run in a browser.
")



#10.3   


#10.3.1   ZeroDivisionError  

# try:
# print(5/0)
# except ZeroDivisionError:
# print(" You can't divide by zero ")

10.3.3
#try-except-else            :

# FileNotFoundError


# try-except 。

filename= 'alice.txt'
try:
with open(filename) as f_obj:##
contens = f_obj.read()## read() contens
except FileNotFoundError:# FileNotFoundError
msg="Sorry , the file " + filename + " does not exist."
print(msg)##

10.3.4 else
#try-except-else            :

print("Give me two numbers , and i'll divide them .")
print("Enter 'q' to quit .")

while True:
first_number=input("
First number: ")
if first_number=='q':
break
second_number=input("
Second number: ")
try: ###### try-except-else ,python try ;
        answer = int(first_number) / int(second_number)###            
except ZeroDivisionError:            #except python, ZeroDivisionError , 。

print("You can't divide by 0!")
else:                       #### try ; try , else
        print(answer)



10.3.6    

# ,
# title = "Alice in Wonderland "
# a=title.split()# 。
# print(a)


10.3.7
def count_words(filename):
" "
try:
with open (filname) as f_obj:
contents= f_obj.read()
except FileExistsError: # , except , pass
msg="Sorry , the file" + filename + " does not exist."
print(msg)
else:
#
words= contents.split()# , : 。
num_words = len(words)
print("The file " + filename + "has about" + str(num_words) + "words")

filename = ['alice.text','siddhartha.txt','moby_dick.txt']
for filename in filenames:
  count_words(filename)

       ,  try-except             :       traceback;                。


10.4


# json python , 。
#json (JavaScriptObject Notation) Javascript 。

 
 
import json
numbers=[2,3,4,56,15]

filename = 'number.json' ## 。json JSON 。
with open(filename,'w')as f_obj:
json.dump(numbers,f_obj)      ## json.dump() : 。
print(numbers)

, load dump 。

import json
filename = 'number.json'
with open(filename) as f_obj:
numbers=json.load(f_obj)      ## json.load() numbers.json , numbers 。

print(numbers)


 
import json

def get_stored_username():
## ,
filename = 'username.json'
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
return None
else:
return username
def greet_user():
# ,
username= get_stored_username()
if username:
print("Welcome back," + username + "!")
else:
username = input("What is your name ?")
filename ='username.json'
with open(filename,'w')as f_obj:
json.dump(username,f_obj)
print("We'll remember you when you come back," + username + "!")

greet_user()



10.4.3


remember_me.py , 。 greet_user(),

### ,



get_stored_username() , ( ), get_stored-username(), 。

!!!!
import json

def get_stored_username():
## ,
filename = 'username.json'
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
return None
else:
return username

def get_new_username():
#
uesrname1=input("What is your name?")
filename='username1.json'
with open(filename,'w')as f_obj:
json.dump(username,f_obj)
return username

def greet_user():
# ,
username= get_stored_username()
if username:
print("Welcome back," + username + "!")
else:
username=get_new_username()
print("We'll remember you when you come back," + username + "!")

greet_user()