zoukankan      html  css  js  c++  java
  • Python学习过程遇到的Bug不断更新

    1、failed to set __main__.__loader__

        兴奋地配置好了Python环境,运行hello.py实例就出现这个异常,着实让人扫兴,百度上搜了下没有找到答案。再去Google了下,发现可能是hello.py文件中包含非英文字符,果然将hello.py放到纯英文路径下就没问题了。

        对于eclipse下使用PyDev的情况,可以用File->Switch Workspace的方法来切换到一个英文路径工作空间目录

    2、_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

         在用下面的代码处理csv文件时出现这个错误(Python 3)

    import csv
    
    def main():
        reader=csv.reader(open('userid.csv', 'rb'))
        for item in reader:
            print(item)
    
    if __name__ == '__main__':
        main()

        经过万能的Google搜索,找到了问题所在:http://bugs.python.org/msg82661 ,下面是部分摘录:

    Sorry, folks, we've got an understanding problem here. CSV files are
    typically NOT created by text editors. They are created e.g. by "save as
    csv" from a spreadsheet program, or as an output option by some database
    query program. They can have just about any character in a field,
    including \r and \n. Fields containing those characters should be quoted
    (just like a comma) by the csv file producer. A csv reader should be
    capable of reproducing the original field division. Here for example is
    a dump of a little file I just created using Excel 2003:
    ...
    This sentence in the documentation is NOT an error: """If csvfile is a
    file object, it must be opened with the ‘b’ flag on platforms where that
    makes a difference."""
     

       虽然这个解释没有告诉我们怎么解决这个问题,但是我根据上面这段话,将代码改成下面这样就OK了:

    import csv
    
    def main():
        reader=csv.reader(open('userid.csv', 'r'))
        for item in reader:
            print(item)
    
    if __name__ == '__main__':
        main()

    3、UnboundLocalError: local variable 'f' referenced before assignment(f.close())

        代码如下:

        # Errors and Exceptions
        # 详细文档参考:http://docs.python.org/2/tutorial/errors.html
        try:
            f = codecs.open("noexistfile.txt", "rU", "utf-8")
            text = f.read()
        except Exception:
            sys.stderr.write('读取文件发生IO异常!\n')
        finally:
            f.close()
            sys.stderr.write('finnaly执行!\n')

       这个错误在打开的文件不存在时才会发生。原因是如果文件不存在则f是None,这时在except语句分支中执行f.close()会报一样的错。这与Java里的文件读取异常处理不太一样,正确的做法如下:

      # Errors and Exceptions
        # 详细文档参考:http://docs.python.org/2/tutorial/errors.html
        try:
            f = codecs.open("noexistfile.txt", "rU", "utf-8")
            text = f.read()
            f.close()
        except Exception:
            sys.stderr.write('读取文件发生IO异常!\n')
        finally:
            sys.stderr.write('finnaly执行!\n')

        其他可能的一种情况:http://blog.csdn.net/magictong/article/details/4464024

        文件读写的推荐写法如下(这样不需要显式关闭文件):

    with open("test.txt", "r") as file_handle:
        for line in file_handle:
           # ...

    ...

  • 相关阅读:
    机器学习作业(八)异常检测与推荐系统——Matlab实现
    机器学习笔记(九)异常检测与推荐系统
    Coursera 吴恩达 机器学习 学习笔记
    机器学习作业(七)非监督学习——Matlab实现
    机器学习笔记(八)非监督学习

    希尔排序
    霍纳算法的散列函数
    javascript判断给定字符串是否是回文
    JavaScript链表
  • 原文地址:https://www.cnblogs.com/feichexia/p/PythonBugs.html
Copyright © 2011-2022 走看看