Chinaunix首页 | 论坛 | 博客
  • 博客访问: 84618
  • 博文数量: 18
  • 博客积分: 1521
  • 博客等级: 上尉
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-22 18:07
文章分类

全部博文(18)

文章存档

2014年(4)

2011年(1)

2010年(4)

2008年(9)

我的朋友

分类: Python/Ruby

2008-12-01 12:19:22

千呼万唤终于开始了了游戏人生!

就以翻译一篇文章开始吧.这是第一次翻译这种技术文章,加上英语很烂,所以只能把大概意思表达出来,如有不对的地方请多多指教.谢谢大家教导.原作是(Peyton McCullough).

1:python文件处理

       在很多应用中,文件操作是一个基本的功能,也是很重要的一部分.相对于其他语言来说,python对文件操作非常简单.peyton McCullough作了以下解释.

       简介:

       在你玩游戏时会使用文件去存储游戏数据,你的命令会存储在文件里.你的报告初稿很明显也是一样存在文件中.

       文件操作在很多用近代语言实现的应用扮演了很重要的角色,在python也不例外.在这篇文章中,我们会探究使用几种模块来操作文件.我们会对文件进行读,写,追加还有其他操作.我们现在就开始.

       读和写:

       从文本中读取数据和把数据写进文本是文本的基本操作.这个非常简单.我们打开一个文本准备写数据:

       fileHandle = open ( "test.txt", "w" )

       "w"表明我们将要把数据写到文本中.剩下的就比较容易理解.下一步实把数据写进文本中:

        fileHandle.write ( ' This is a test. \nReady, it is. ' )

        这就把字符串"This is a test."写进文本的第一行,"Really , it is."写到了第二行.最后,我们需要清理和关闭文本.

         fileHandle.close( )

         正如你所看到的,这个很容易,特别是对python的对象.但要清楚,当你使用"w"模式去再次写数据到文本中的时候,文本中的所有内容都会背删除掉.为了解决这个问题,可以使用"a"模式去把数据追加到文本末尾,添加数据到末尾:

          fileHandle = open ( ' test.txt ', ' a ' )

          fileHandle.write ( ' \n\n\nBottom line. ' )

          fileHandle.close (  )

          现在我们把文本的数据读出来并显示:

          fileHandle = open ( ' test.txt ' )

          print  fileHandle.read ( )

          fileHandle.close( )

          这把文本中数据全部读取出来并显示出来.我们也可以读取文本中的一行数据:

          fileHandle = open ( ' test.txt ' )

          print fileHandle.readline ( ) # " This is a test . "

          同样也可以把文本中的所有行存储到一个list中:

           fileHandle = open ( ' test.txt ' )

           fileList = fileHandle.readlines( )

           for fileline in fileList :

                 print '>>', fileline

           fileHandle.close( )

           当在从文本中读取数据时,Python会记住指针在文本中的位置,如下例子:

           fileHandle = open ( ' test.txt ' )

           garbage = fileHandle.readline( )

           fileHandle.readline ( ) # "Really, it is. "

           fileHandle.close ( )

           只有第二行显示出来.当然,我们也可以通过把指针定位到其他地方来读取数据.

           fileHandle = open ( 'test.txt' )

           garbage = fileHandle.readline ( )

           fileHandle .seek ( 0 )

           print  fileHandle.readline ( ) # " This is a test . "

           fileHandle.close ( )

           由上面的例子可以知道,我们可以告诉python继续从文本的第一个字节开始读取数据. 因此,第一行数据被打印出来. 我们同样可以要求python告诉指针当前位置:

           fileHandle = open ( ' test.txt ' )

           print  fileHandle.readlien ( )  # " This is a test "

           print  fileHandle .tell ( 0 )  # " 17 "

           print fileHandle.readline( )   # " Really , it is "   

          同样,一次也可以读取指定字节数的数据:

          fileHandle  = open ( ' test.txt ' )

          print fileHandle ( 1 ) # " T "

          fileHandle.seek ( 4 )

          print fileHandle.read ( 1 ) # " T "

          当我们在Windows 和 Macintosh平台时,有时候可以需要以二进制的模式来写数据,比如图片文件.为了做到这点,只要以"b"模式打开文本:

          fileHandle = open ( ' testBinary.txt ', ' wb ' )

          fileHandle.write ( ' There is no spoon. ' )

          fileHandle.close ( )

          fileHandle = open ( ' testBinary.txt ' , ' rb ' )

          print fileHandle.read ( )

          fileHandle .close ( )

阅读(2569) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~