`

Python,how do you work (二)

 
阅读更多

关于python,我上一篇说了一些好的例子和在python中怎么创建class的方法和class的内部方法,构造方法,析构方法等等

 

接下来我将说明怎么python对IO的操作

 

其实python的文件操作有点像c语言,说实话。感觉比c方便的多。

 

举例说明一切。

 

使用file类的read,readline或者wirte方法来执行读写操作

 

poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
        use Python!
'''

f = file('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file

f = file('poem.txt')
# if no mode is specified, 'r'ead mode is assumed by default
while True:
    line = f.readline()
    if len(line) == 0: # Zero length indicates EOF
        break
    print line,
    # Notice comma to avoid automatic newline added by Python
f.close() # close the file

 

 感觉很简单吧。

 

 

python还有储存器

 

cPickle就是储存器

 

下里是例子:

 

import cPickle as p
#import pickle as p

shoplistfile = 'shoplist.data'
# the name of the file where we will store the object

shoplist = ['apple', 'mango', 'carrot']

# Write to the file
f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()

del shoplist # remove the shoplist

# Read back from the storage
f = file(shoplistfile)
storedlist = p.d(f)
print storedlist
 

 

 

 

0
7
分享到:
评论

相关推荐

    Learn Data Analysis with Python 英文原版

    If you are already using Python for data analysis, you will find a number of things that you wish you knew how to do in Python. You can then take these techniques and apply them directly to your own ...

    Data Wrangling with Python 2016新书 无水印pdf 0分

    How do you take your data analysis skills beyond Excel to the next level? By learning just enough Python to get stuff done. This hands-on guide shows non-programmers like you how to process ...

    Python Advanced Predictive Analytics

    Data is powerful but not in its raw form - It needs to be processed and modeled, and Python is one of the most robust tools out there to do so. It has an array of packages for predictive modeling and...

    Learning Python

    Then, the authors address the mechanics of the language itself, providing illustrations of how Python conceives of numbers, strings, and other objects as well as the operators you use to work with ...

    Python 2.1 Bible .pdf

    how you endured your pre-Python days. About This Book Although Python is a great first programming language, in this book we do assume that you already have some programming experience. The first ...

    Effective-Python-Development-for-Biologists.pdf

    - Environments for development - learn how you can take advantage of different tools for actually writing code, including those designed specifically for scientific work. - Organising and sharing code...

    [machine_learning_mastery系列]Machine_Learning_Mastery_With_Python.pdf

    I am often asked the question: How do you use Python for machine learning? This book is my definitive answer to that question. It contains my very best knowledge and ideas on how to work through ...

    Lean.Python.Learn.Just.Enough.Python.to.Build.Useful.Tools

    You work in IT and need a programming primer – you might be a tester who needs to have more informed technical discussions with programmers. Working through the examples will help you to appreciate ...

    Automate.the.Boring.Stuff.with.Python.Practical.Programming

    In Automate the Boring Stuff with Python, you'll learn how to use Python to write programs that do in minutes what would take you hours to do by hand—no prior programming experience required....

    笨方法学python3 Learn Python 3 the Hard Way

    As you do, you’ll learn how a computer works; what good programs look like; and how to read, write, and think about code. Zed then teaches you even more in 5+ hours of video where he shows you how ...

    Machine.Learning.in.Python

    Machine Learning in Python shows you how to do this, without requiring an extensive background in math or statistics. Table of Contents Chapter 1 The Two Essential Algorithms for Making Predictions ...

    Python安装包version 3.1.5

    especially how built-in objects like dictionaries and strings work, have changed considerably, and a lot of deprecated features have finally been removed. Build Instructions ------------------ On ...

    Python 3 Text Processing with NLTK 3 Cookbook

    This book is intended for Python programmers interested in learning how to do natural language processing. Maybe you've learned the limits of regular expressions the hard way, or you've realized that ...

    Geoprocessing-with-Python.pdf

    made me realize how much I enjoyed working with Python, so naturally I had to start using GDAL with it as well. More importantly for this book, my coworker John Lowry suggested that we team- teach a ...

    Python.for.Secret.Agents.2nd.Edition.1785283405

    Python is easy to learn and extensible programming language that allows any manner of secret agent to work with a variety of data. Agents from beginners to seasoned veterans will benefit from Python's...

    Effective Python Development for Biologists pdf

    Environments for development - learn how you can take advantage of different tools for actually writing code, including those designed specifically for scientific work. Organising and sharing code - ...

    Machine Learning in Python 无水印pdf 0分

    Machine Learning in Python: Essential Techniques for Predictive Analysis ... Machine Learning in Python shows you how to do this, without requiring an extensive background in math or statistics.

    OpenCV 3.x with Python By Example, 2nd Edition-Packt Publishing(2018).pdf

    Computer vision is found everywhere in ... By the end of this chapter, you will be able to understand how neural networks work and how to apply them to machine learning to build advance images tools.

    Udemy - Deep Learning Convolutional Neural Networks in Python

    We will also do some biology and talk about how convolutional neural networks have been inspired by the animal visual cortex. After describing the architecture of a convolutional neural network, we ...

    Packtpub.Python.2.6.Text.Processing.Beginners.Guide.Dec.2010

    If this is your life, this book will make it better – a practical guide on how to do what you want with textual data in Python. Python 2.6 Text Processing Beginner’s Guide is the easiest way to ...

Global site tag (gtag.js) - Google Analytics