Python---Error notes

Source: Internet
Author: User

Python---error note 1. Python Coding issues:when we write a Python script, we often write Chinese comments. But sometimes, when we run the program, we find the following error: Syntaxerror:non-ascii character ' \xe5 ' in File/home/johnnie/files/workspace/python/head_ first/chapter2/nester.py on line 7, but no encoding declared; See http://python.org/dev/peps/pep-0263/for details.The reason: is the problem of Chinese encodingSolution:
Method 1: Add the following in the file: #!/usr/bin/python or #!user/bin/env python

#encoding =utf-8


#-*-Coding:utf-8-*-# recommended this way: You can solve the problem of importing Chinese between modules, it is recommended to write each time [Note that the declared encoding must be consistent with the encoding used when the file is actually saved, otherwise there is a large chance of a code parsing exception. Now the IDE will generally automatically handle this situation, change the declaration and then replace the declaration of the encoding to save, but the text editor controls need to be careful]
2. python file read error:in the Run read_file.py Program, encountered an error: need more than 1 value to unpack
Cause : Line.split (":", 1) error caused by this statementSolution: We should check if the file we want to read has a blank line at the end, delete the empty row Read_file program code:
#!/usr/bin/python#encoding=utf-8# filename:read_file.py# Read data import Osdata_path = "/home/johnnie/document/workspace/ Python/data/sketch.txt "If Os.path.exists (data_path):    data = open (Data_path) while    True: for line        in Data.readlines ():            If Len (line) = = 0: Break            Else:                # Adds a logical judgment: see if the current data row contains a ":" character, and if so, split the data                # find (): Finds a substring in a string that, if not found, returns 1 # not                : The expression is reversed                if not Line.find (":") = =-1:                    try:                        (role, msg) = Line.split (":", 1 )                        print ("{0} Said:{1}"). Format (role, MSG)                    except ValueError:                        pass    Data.close () Else: # IOError    print "The data file is missing!"

3. The Python Web server runs the CGI foot of this newspaper FileNotFound error:When the WEB server runs a CGI script that encounters filenotfounderror: [Errno 2] No such file or directory, don't worry, first look at:
1). Do you really have the CGI script?
2). Do you want to give the script permission to execute?
3). One of the most overlooked is that the first line of the CGI script must include the following code:
#!/usr/bin/python or #!/usr/bin/python3 instead of #!usr/bin/python [ note : USR is preceded by a left slash!!!!] ]

4. The {%url%} tag error is used in the Django template:using the {% URL%} tag in the href attribute of the <a> tag in the Django template file blog_detail.html, the following error occurred: Arguments ' () ' and keyword Argumen ts ' {} ' not found .....source code fragment, root directory urls.py:
Urlpatterns = [..., url (r ' ^sblog/blog_details/(\d+) ', ' Sblogs.views.blog_detail '),]
blog_details.html:
..... <a href = "{% url ' sblog/blog_details ' blog.id%}" >....</a>
reason for the occurrence:incorrect use of the configuration URLs and tags. First, when using the {% URL%} tag, it is recommended that for each app, the
Create a new urls.py file in the app directory to manage the URL of the app. And the URL should be defined as follows: URL (r ' ^accounts/logout/$ ', logout_view, name = ' Logout_view ')Solution:1) Modify the urls.py in the root directory:
urlpatterns = [url (r ' ^admin/', include (Admin.site.urls)), url (r ' ^$ ', ' blog.views.home ', name= "Home"),        # recursively configure A urls.py    url (r ' ^sblog/', include (' Sblog.urls ')) is configured under Url:sblog,]
2) Create a new urls.py under the app:
Urlpatterns = Patterns ((' sblog.views '),                url (r ' ^blog_list ', "blog_list", Name= "Blog_list"),            url (r ' ^blog_ detail/(\d+) ', ' Blog_detail ', name= ' Blog_detail '),            )
3) Modify the blog_detail.html file:
<a href= "{% url ' blog_detail ' blog.id%}" >{{blog.caption}}</a>



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python---Error notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.