在很多时候,你会想要让你的程序与用户(可能是你自己)交互。你会从用户那里得到输入,然后打印一些结果。我们可以分别使用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输入和输出的更多相关文章

  1. Python - 输入和输出 - 第十七天

    Python 输入和输出 在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能.本章节我们将具体介绍 Python 的输入输出. 输出格式美化 Python两种输出值的方式: 表达式语 ...

  2. python 输入和输出

    到目前为止我们遇到过两种输出值的方法: 表达式语句和print语句. (第三个方式是使用文件对象的write()方法: 标准输出文件可以引用 sys.stdout.详细内容参见库参考手册. Pytho ...

  3. 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 ...

  4. Python输入与输出

    输出 print函数 语法: print(self, *args, sep=' ', end='\n', file=None) print函数是python中最常见的一个函数.用于将内容打印输出. p ...

  5. Python 输入与输出

    Python2版本 raw_input raw_input("输入提示"),会把输入的内容当做字符串返回 input 会把用户输入的内容当做代码来处理,可以理解为 raw_inpu ...

  6. Ulipad Python输入先后输出问题

    print "Enter a interger"number=input() 在菜单栏 python-----设置参数----在Parameters:那栏加个参数 -u , 就可以 ...

  7. python输入一个\输出2个\问题

    在Python里面,如果\后面不是一个合法的转移字符,那么,Python会打印两个\,换句话说,Python将\也当成普通字符看待,而不是转义符的标志: >>>S = 'C:\py\ ...

  8. Python学习--02输入和输出

    命令行输入 x = input("Please input x:") y = raw_input("Please input x:") 使用input和raw_ ...

  9. Python 基础【第三篇】输入和输出

    这里我们创建一个python(pytest)脚本用于学习测试(以后都为这个文件,不多做解释喽),这个文件必须要有执行权限的哈 1.创建pytest并赋予执行权限 [root@fengyuba_serv ...

随机推荐

  1. poj 3264 线段树 求区间最大最小值

    Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...

  2. ZOJ 1076 Gene Assembly

    原题链接 题目大意:首先学习一个生物学的单词,exon:外显子,DNA序列中能够翻译表达的片段.给出很多外显子的起始点和终点,求寻找包含最多外显子的一条链,并且输出这些外显子的编号. 解法:先把所有外 ...

  3. c 函数及指针学习 5

    聚合数据类型 能够同时存储超过一个的单独数据. c语言提供了数组和结构体. 1. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> # ...

  4. UVa 442 矩阵链乘(栈)

    Input Specification Input consists of two parts: a list of matrices and a list of expressions. The f ...

  5. leetcode 97 Interleaving String ----- java

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  6. PHP设置图片文件上传大小的具体实现方法

    PHP默认的上传限定是最大2M,想上传超过此设定的文件,需要调整PHP.apache等的一些参数 我们简要介绍一下PHP文件上传涉及到的一些参数: •file_uploads :是否允许通过HTTP上 ...

  7. POJ1419 Graph Coloring(最大独立集)(最大团)

                                                               Graph Coloring Time Limit: 1000MS   Memor ...

  8. 关于ZF2中一点感悟,service_manager

    在zf2中,在serviceLoctor中自定义的内容,可以通$serviceLocator->get('config')['key'],如果是在serivce_manger中定义的服务名,其实 ...

  9. hdu5443(2015长春赛区网络赛1007)暴力

    题意:给了一个数列,有多个询问,每个询问求某个区间内的最大值 数列长度 1000,询问个数 1000,静态,并不需要RMQ这些,直接暴力 n2 查找每个询问区间取最大值就行了. #include< ...

  10. SUID,SGID,Sticky Bit详解(转)

    SUID属性 passwd命令可以用于更改用户的密码,一般用户可以使用这个命令修改自己的密码.但是保存用户密码的/etc/shadow文件的权限是400,也就是说只有文件的所有者root用户可以写入, ...