在CSV文件中添加联系人的python方法
学习Python有一段时间了,想自己动手写点什么,想到了一个比较简单的,就是在CSV文件中增加联系人的这个操作。
在CSV文件中添加联系人的python方法
import sys import os import time import csv def addPerson(filename): '''Add a new Person\'s Tel''' person = raw_input('Enter the person\'s name: ') tel = raw_input('Enter the person\'s tel: ') update=time.strftime('%Y-%m-%d %H:%m:%S') f=csv.writer(file(filename, 'a')) f.writerow([person,tel,update]) #f.close() print 'New Person\'s tel has been added!' if (os.path.isfile('TelBook.csv'))==False: title=['NAME','TEL','TIME'] f=csv.writer(file('TelBook.csv','w')) f.writerow(title) #f.close() input=raw_input('Do you wanna Enter a new Person\' tel?(y/n)') if input=='y': flag=True else: flag=False print 'Thanks, Bye!' while flag: addPerson('TelBook.csv') t=raw_input('Do you wanna Enter another?(y/n)') if t!='y': print 'Thanks, Bye!' break #www.iplaypy.com
这部分是修改了csv文件中多一空行的问题
import sys import os import time import csv def addPerson(filename): '''Add a new Person\'s Tel''' person = raw_input('Enter the person\'s name: ') tel = raw_input('Enter the person\'s tel: ') update=time.strftime('%Y-%m-%d %H:%m:%S') f=csv.writer(file(filename, 'ab')) record=[person,tel,update] f.writerow(record) #f.close() print 'New Person\'s tel has been added!' if (os.path.isfile(r'D:\Exercise\Python\TelBook\TelBook.csv'))==False: title=['NAME','TEL','TIME'] #f=csv.writer(file(r'D:\Exercise\Python\TelBook\TelBook.csv','w')) f=csv.writer(file(r'D:\Exercise\Python\TelBook\TelBook.csv','wb')) f.writerow(title) file(r'D:\Exercise\Python\TelBook\TelBook.csv').close() input=raw_input('Do you wanna Enter a new Person\' tel?(y/n)') if input=='y': flag=True else: flag=False print 'Thanks, Bye!' while flag: addPerson(r'D:\Exercise\Python\TelBook\TelBook.csv') t=raw_input('Do you wanna Enter another?(y/n)') if t!='y': print 'Thanks, Bye!' break file(r'D:\Exercise\Python\TelBook\TelBook.csv').close()
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/other/o2332.html
相关文章 Recommend
- • python re模块中的 match()和group()疑问
- • Netbeans 7.0.0.1安装python插件安装后,启动IDE显示不
- • 如何为实时性应用存取经纬度?django mysql
- • 又是python乱码问题,excel类型文件单元格内数据有
- • 请教web.py服务器端接收到的上传文件名出现乱码
- • 使用django在做添加superuser操作时报错\xBA\xA3像是乱
- • python语言中的pcap与dpkt在统计中的作用分别是什么
- • django-1.x windows环境 Error: 'SafeUnicode' object has no a
- • 环境部署问题,python web应用可以在代码中判断de
- • Python语言中pyAudio模块做录音文件效果样
- • linux系统 修改include出现问题
- • 在生产环境中thrift Python server如何选择
我要分享到:
必知PYTHON教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
必知PYTHON模块 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • python爬虫获取腾讯新闻正文标题内容
- • 用python xmlrpc调用实现发布博文
- • 拉手网Python程序员面试题
- • 随机双色球中奖号码python代码
- • python爬取百度云网盘资源
- • python monkeyrunner 代码示例
- • 匹配IP与匹配 domain python代码源码
- • Python调用MySQL存储过程,获得返回值示例
- • 如何用python方法检测UDP端口
- • 动态filter查询数据Django实现方法
图文精华 RECOMMEND
-
Python调用MySQL存储过程,获得返回值
-
python抓取局域网内消耗网络带宽最
-
wxPython系统剪切板使用方法
-
pyhooks方法实现键盘监控源码示例
-
web表单提交GET和POST方法示例
-
python实现whois查询功能的方法
热点文章 HOT
- Python urllib2发送即时消息到twitter的实现方
- Python方法调用网站接口实现MD5值查询
- 如何让django接入新浪微博OAuth的方法
- 动态filter查询数据Django实现方法
- python中apt简单使用工具
- Python代码片段之Django静态文件URL的配置
- 初学者用python爬凡客图片的源码
- pyhooks方法实现键盘监控源码示例