《python程序设计教程》第7章 文件操作 习题7答案

1.编写程序生成九九乘法表,并将之写入到文本文件exercise7_1.txt中。程序保存为exercise7_1.py。

f = open(r'd:\exercise7_1.txt', 'a+')
with open('d:\\exercise7_1.txt','a+') as f:
    for j in range(1,10):
        for i in range(1,j+1):
            f.write(str(i)+'*'+str(j)+ '=' + str(i*j) + '\t')
        f.write('\n')
f.close()

2.编写程序,提示用户输入字符串。将所输入的字符串,以及对应字符串的长度写入exercise7_2.txt中。程序保存为exercise7_2.py。

f = open(r'd:\exercise7_2.txt', 'a+')
with open('d:\\exercise7_2.txt','a+') as f:
    s=input("请输入一个字符串:")
    f.write("输入的字符串是:" + str(s) +"\n"+ "字符串的长度是:" + str(len(s)))
f.close()

3.创建一个exercise7_3.xlsx文件,在C5单元格写入字符串“我喜欢编程”。程序保存为exercise7_3.py。

import openpyxl
workbook=openpyxl.Workbook()
sheet1=workbook.active
sheet1.cell(row=5,column=3).value="我喜欢编程"
workbook.save("d:\\exercise7_3.xlsx")

  • 9
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值