1. [python] view plaincopyprint?  
  2. [wu@server python]$ cat ex4.py     
  3. #!/usr/bin/python     
  4. # -*- coding:utf-8 -*-     
  5.     
  6. #中文注释     
  7. #cars_num     
  8. cars = 100    
  9. #space_car_num     
  10. space_in_a_car = 40    
  11. drivers = 30    
  12. passengers = 90    
  13. carscars_not_driven = cars - drivers    
  14. cars_driven = drivers    
  15. carpool_capacity = cars_driven * space_in_a_car    
  16. average_passengers_per_car = passengers / cars_driven    
  17.     
  18. print "There are", cars, "cars available."    
  19. print "There are only", drivers, "drivers available."    
  20. print "There will be", cars_not_driven, "empty cars today."    
  21. print "We can transport", carpool_capacity, "people today."    
  22. print "We have", passengers, "to carpool today."    
  23. print "We need to put about", average_passengers_per_car, "in each car."    
  24. [wu@server python]$ ./ex4.py     
  25. There are 100 cars available.    
  26. There are only 30 drivers available.    
  27. There will be 70 empty cars today.    
  28. We can transport 1200 people today.    
  29. We have 90 to carpool today.    
  30. We need to put about 3 in each car.    
  31. [wu@server python]$  

在RHEL6下使用Vim写python脚本发现不能使用中文注释,后来发现是编码问题

 
  
  1. [python] view plaincopyprint?  
  2. [wu@server python]$ cat ex4.py     
  3. #!/usr/bin/python     
  4.     
  5. #中文注释     
  6. #cars_num     
  7. cars = 100    
  8. #space_car_num     
  9. space_in_a_car = 40    
  10. drivers = 30    
  11. passengers = 90    
  12. carscars_not_driven = cars - drivers    
  13. cars_driven = drivers    
  14. carpool_capacity = cars_driven * space_in_a_car    
  15. average_passengers_per_car = passengers / cars_driven    
  16.     
  17. print "There are", cars, "cars available."    
  18. print "There are only", drivers, "drivers available."    
  19. print "There will be", cars_not_driven, "empty cars today."    
  20. print "We can transport", carpool_capacity, "people today."    
  21. print "We have", passengers, "to carpool today."    
  22. print "We need to put about", average_passengers_per_car, "in each car."    
  23. [wu@server python]$ ./ex4.py    
  24.   File "./ex4.py", line 3    
  25. SyntaxError: Non-ASCII character '\xe4' in file ./ex4.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details  

加上# -*- coding:utf-8 -*-之后就能成功使用中文注释了