(Python学习)CSV电话本,python学习csv电话,CSV电话本#Filen
(Python学习)CSV电话本,python学习csv电话,CSV电话本#Filen
CSV电话本
#Filename: TelBook.pyimport 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=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
修改了csv文件中多一空行的问题
#Filename: TelBook.pyimport 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=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()
相关内容
- 一段阅读英语的小代码,一段阅读英语代码,[Python]代码
- 遍历文件,遍历,[Python]代码im
- 猜数字游戏,,[Python]代码im
- Python实现的Blowfish加解密,pythonblowfish,Blowfish加解密字
- 又是 web.py 页面执行计时,web.py页面执行计时,实现计时
- python code for solving eigenvalue problem by Jacobi’s method,eig
- python queue模块 消息队列,pythonqueue,#!/usr/local
- 使用python3的base64编解码实现字符串的简易加密解密,
- 输出金字塔形数字,输出金字塔形,[Python]代码i=
- python分割文件,,[Python]代码#!
评论关闭