在CSV文件中添加联系人的python方法,csvpython,学习Python有一段时
在CSV文件中添加联系人的python方法,csvpython,学习Python有一段时
学习Python有一段时间了,想自己动手写点什么,想到了一个比较简单的,就是在CSV文件中增加联系人的这个操作。
在CSV文件中添加联系人的python方法
import sysimport osimport timeimport csvdef 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=Trueelse:flag=Falseprint '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 sysimport osimport timeimport csvdef 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=Trueelse:flag=Falseprint '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!'breakfile(r'D:\Exercise\Python\TelBook\TelBook.csv').close()
编橙之家文章,
相关内容
- Python Flask方法实现Whois域名查询功能,flaskwhois,Python F
- 用python sched模块周期抓取论坛在线人线示例,pythonsch
- Python方法调用网站接口实现MD5值查询,pythonmd5,这是在
- Python对多线程数量的限制方法,python多线程数量,当你在
- 一个简易的hash加密小工具源码,hash加密源码,一直喜欢
- 计算前一个月最后一天日期的python方法,最后一天pyt
- python计算上个月第一天日期的方法,python上个月,pytho
- 计算上个月有多少天的python简单小程序,上个月python
- django实现获取用户输入方法,django实现获取用户,这是用
- python自动登录并备份文件操作,python自动登录备份,py
评论关闭