Python输入和输出
在很多时候,你会想要让你的程序与用户(可能是你自己)交互。你会从用户那里得到输入,然后打印一些结果。我们可以分别使用raw_input和print语句来完成这些功能。对于输出,你也可以使用多种多样的str(字符串)类。例如,你能够使用rjust方法来得到一个按一定宽度右对齐的字符串。利用help(str)获得更多详情。另一个常用的输入/输出类型是处理文件。创建、读和写文件的能力是许多程序所必需的,我们将会在这章探索如何实现这些功能。
1.使用文件
#!/usr/bin/python
# Filename: using_file.py
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
运行结果
# ./using_file.py
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
2.储存与取储存
#!/usr/bin/python
# Filename: pickling.py
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.load(f)
print storedlist
运行结果
# ./pickling.py
['apple', 'mango', 'carrot']
为了在文件里储存一个对象,首先以写模式打开一个file对象,然后调用储存器模块的dump函数,把对象储存到打开的文件中。这个过程称为 储存 。接下来,我们使用pickle模块的load函数的返回来取回对象。这个过程称为 取储存 。
Python输入和输出的更多相关文章
- Python - 输入和输出 - 第十七天
Python 输入和输出 在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能.本章节我们将具体介绍 Python 的输入输出. 输出格式美化 Python两种输出值的方式: 表达式语 ...
- python 输入和输出
到目前为止我们遇到过两种输出值的方法: 表达式语句和print语句. (第三个方式是使用文件对象的write()方法: 标准输出文件可以引用 sys.stdout.详细内容参见库参考手册. Pytho ...
- python输入与输出165
s = 'Hello,Runoob' print(s) str(s) print(s) print(repr(s)) print(1/7) print(str(1/7)) print(repr(1/7 ...
- Python输入与输出
输出 print函数 语法: print(self, *args, sep=' ', end='\n', file=None) print函数是python中最常见的一个函数.用于将内容打印输出. p ...
- Python 输入与输出
Python2版本 raw_input raw_input("输入提示"),会把输入的内容当做字符串返回 input 会把用户输入的内容当做代码来处理,可以理解为 raw_inpu ...
- Ulipad Python输入先后输出问题
print "Enter a interger"number=input() 在菜单栏 python-----设置参数----在Parameters:那栏加个参数 -u , 就可以 ...
- python输入一个\输出2个\问题
在Python里面,如果\后面不是一个合法的转移字符,那么,Python会打印两个\,换句话说,Python将\也当成普通字符看待,而不是转义符的标志: >>>S = 'C:\py\ ...
- Python学习--02输入和输出
命令行输入 x = input("Please input x:") y = raw_input("Please input x:") 使用input和raw_ ...
- Python 基础【第三篇】输入和输出
这里我们创建一个python(pytest)脚本用于学习测试(以后都为这个文件,不多做解释喽),这个文件必须要有执行权限的哈 1.创建pytest并赋予执行权限 [root@fengyuba_serv ...
随机推荐
- 【转】ChainMapper 实例理解一
通过ChainMapper可以将多个map类合并成一个map任务. 下面个这个例子没什么实际意思,但是很好的演示了ChainMapper的作用. 源文件100 tom 90101 mary 85102 ...
- jbrowse 的配置与使用gff, vcf, fa, bed, bam
1,jbrowse 是什么东西 ? JBrowse is a genome browser with a fully dynamic AJAX interface, being developed a ...
- SourceTree不出现用户登录窗口,提示错误fatal: unable to access'...'; error setting certificate verify locations
SourceTree不出现用户登录窗口,提示错误fatal: unable to access'...'; error setting certificate verify locations; .. ...
- 在Linux上使用web2py_uwsgi_nginx搭建web服务器
本文介绍在Linux使用Python+Nginx+web2py+uWSGI搭建一个web服务器的过程. Python 2.7.11 解压安装包 tar -zxvf Python-2.7.11.tgz ...
- dede模版列表调用文章正文内容的方法
在制作织梦模板的时候,有的时候我们需要调用文章部分内容,用[field:description/]标签字数不够多(数据库设计字段是varchar(255)的),另外修改了文章内容但是摘要还需要手动修改 ...
- ajax 异步调用把返回值赋给一个全局变量的用法,最主要的就是把async属性改为 false,
<script> $(document).ready(function () { <% string dqsj = System.DateTime.Now.ToString(&quo ...
- 51nod 最大子矩阵和(动态规划)
最大子矩阵和 一个M*N的矩阵,矩阵中有一些整数(有正有负),找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值. 输入 第1行:M和N,中间用空格隔开(2 <= M,N ...
- HDU-3308 LCIS(区间合并)
题目大意:给一个整数序列,m次询问,每次询问某个区间中最长连续上升子序列的长度. 题目分析:线段树区间合并.维护以区间左端开头的.以区间右端点结尾的和区间最长的上升连续序列. 代码如下: # incl ...
- PHP内存消耗
由于变量占用的空间不一样,所以其消耗的内存大小也不一样,在PHP中我们可以通过使用“memory_get_usage”来获取当前PHP消耗的内存.但是根据操作系统.PHP版本以及PHP的运行方式可能输 ...
- DuiLib——第二篇UIBase
---------------------------------------------------------------------------------- 分析约定: private o-- ...