编程自己常见error(1)

** 最近学习了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’

注意关掉占用就好。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章