python 新建文件怎么加后缀

python 新建文件怎么加后缀,第1张

建议:

* 使用with语句读取文件

* 函数方式调用print以兼容py3

* pep8命名规则命名变量

#coding=utf8

import easygui

print(u"你好,欢迎使用充电数据表。")

print(u"记账功能,查账功能")

choice = easygui.buttonbox(msg = u"选择需要使用的功能",

title = u"充电数据表",

choices = (u"查账",u"记账",u"撤销"))

if choice == u"查账" :

while 1:

roomNumber = easygui.enterbox(msg = u"输入房间号码")

if not roomNumber.isdigit():

print(u'请输入正确的房间号')

continue

#输入房号

print(u"%s房"%roomNumber)

roomNumber = int(roomNumber)

break

#**************************************************************

try:#try...expect是python中的异常处理语句,try中写

with open('%s.txt'%roomNumber) as f: # 待检测的 *** 作语句

for line in f: print line

except IOError: # expect中写差错处理语句

print '*** file open error:'

raw_input(u'按回车关闭\Press Enter to close')

#*****************************************************************

17*[th]

生成

一个长度为17的列表元素均为'th'。+,将列表合并成一个大的列表。所以这行代码的作用时生成一个长度为31的后缀字符数组。这样就可以在day数字(1-31)后面

相应地加上后缀。如(1st,

2nd,

3rd,

4th,

5th,

,

19th,

20th,

21st,

22nd,

22rd,

23rd,

24th,

,

30th,

31st)。

>>>endings=['st','nd','rd']+17*['th']+['st','nd','rd']+7*['th']+['st']

>>>print endings

['st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st']

python中的list叫做列表,可以通过append方法在列表的末尾添加单个元素

x = [1,2,3]

x.append(4)

或者使用extend方法在列表末位添加多个元素,参数就变成了列表

x.append([4,5,6])

或者使用insert方法在任意位置添加元素,第一个参数是插入元素的位置,第二个参数是插入元素的值

x.insert(0,-1)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11497175.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存