** 最近學習了Python, 剛剛學習了兩週的時間,學習的書籍是 Learn Python the hard way.幾乎就要學完,在學習的過程中遇到的很多問題希望記錄下來,和大家分享,也算是對自己學習的總結!**
我將學習過程中遇到的BUG 做了一下總結。我將這個連續總結大概四篇到五篇的樣子吧!
編程自己常見error(1)
1 、編碼沒有中文,產生的問題,以及解決方法
print "Mary had a little lamb." # This is characters
print "Its fleece was white as %s." % 'snow' # This is anthor string
print "And everywhere that Mary went." # Third string
print "." * 10 # What'd that do? # Let me see what happen.
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r" # 12 alphalets
# watch that comma at the end. try removing it to see what happens
print end1 + end2 + end3 + end4 + end5 + end6 ,
print end7 + end8 + end9 + end10 + end11 + end12 # is so important
File "ex7.py", line 21
SyntaxError: Non-ASCII character '\xef' in file ex7.py on line 21, but no encoding
declared; see http://python.org/dev/peps/pep-0263/ for details
PS C:\users\15222\lpthw>
註釋是中文狀態 統統不可以 謹記!!
於是我自己通過網上搜索方法,發現可以重新編碼進行解決這個問題。
#-*- coding:utf-8 –*-
#在Python中顯示中文註釋和輸出中文·
然後我們進行對應的代碼改寫就好:
a ="中文"
print a.decode('utf-8').encode('cp936')
返回結果爲:
返回結果:
d:\Python27\python.exe "D:\test\中文.py"
Process started >>>
中文
這是ex8.py中我進行的改進,代碼是:
#-*-coding:utf-8-*-
#在pYthon中顯示中文註釋和輸出中文
a = "中文"
print a.decode('utf-8').encode('cp936')
b = '夢想'
formatter = "%r %r %r %r"
print formatter % ( 1, 2, 3, 4 )
print formatter % ("one", "two", "three", "four" )
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % ("I had this thing.", "That you could type up right.", "But it didn't sing.", "So I had goodnight.")
print "I had a " + b.decode('utf-8').encode('cp936')
輸出結果是:
中文
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I had goodnight.'
I had a 夢想
2、標誌字符串的格式
標誌字符串 formatter 注意%前邊是沒有,(逗號)。這種細節錯誤容易犯,而且不易察覺。
代碼示例:
formatter = "%r %r %r %r"
print formatter ,% ( '1',' 2', '3',' 4' )
這段代碼%前邊不小心添加了逗號,導致出現語法錯誤:
PS C:\Users\15222\lpthw> python ex8.py
File "ex8.py", line 9
print formatter ,% ( '1',' 2', '3',' 4' )
^SyntaxError: invalid syntax
正確語法:
formatter = "%r %r %r %r"
print formatter % ( '1',' 2', '3',' 4' )
3、學習參數變量時候遇到的問題
做練習13的時候 看書不仔細 沒有看明白具體意思 ,造成錯誤。
命名 了四個變量 結果 僅僅輸入了一個變量 造成報錯。
錯誤示例(代碼正確,調用方式錯誤):
from sys import argv
script ,first, second,third, si = argv
#script = raw_input("jiaoben:")
#first = raw_input("diyigeshuju:")
print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third
print "Your si variable is:", si
調用這個腳本的時候(錯誤示例 ):
PS C:\Users\15222\lpthw> python ex13.py nihao
Traceback (most recent call last):
File "ex13.py", line 3, in <module>
script ,first, second,third, si = argv
ValueError: need more than 2 values to unpack
應該是需要除了腳本名稱之外的四個變量名稱,結果僅僅給出了1個,遠遠不夠,產生報錯。
正確調用方法:
PS C:\Users\15222\lpthw> python ex13.py nihao nibuhao nzhendehao hahah
The script is called: ex13.py
Your first variable is: nihao
Your second variable is: nibuhao
Your third variable is: nzhendehao
Your si variable is: hahah
總結:python ex13.py 這裏應該輸入四個變量的,但是僅僅輸入一個。
4、利用命令行產生測試文本文件的時候產生的問題
在Powershell中敲好命令行:
PS C:\Users\15222\lpthw> echo "nihao">zzc.txt
PS C:\Users\15222\lpthw> cat zzc.txt
nihao
產生包含對應內容的文本文件。
當我利用Python腳本打開文件,讀取文件內容時,產生了這種效果:
PS C:\Users\15222\lpthw> python ex15.py zzc.txt
Here's your file 'zzc.txt':
i h a o
Type the filename again:
> zzc.txt
i h a o
產生這種效果的原因應該是編碼不一致造成的。
當我重新創建文件,並手動寫入文本時,產生的效果如下:
PS C:\Users\15222\lpthw> python ex15.py haha.txt
Here’s your file ‘haha.txt’:
nihao
Type the filename again:
haha.txt
nihao
完全沒有任何問題。
5、利用Python對文件進行操作的問題
注意我們 進行讀寫的文件是不可以用其他軟件打開的 不然就會報錯,一開始我還沒有意識到是這個問題,試了好久發現是文件被佔用了…
IOError: [Errno 13] Permission denied: ‘text.txt’
注意關掉佔用就好。