核心编程第九章(下)

9-15

def copyfile():
    f1 = open('yanyuxing.txt')
    f2 = open('yyx.txt', 'a')
    f2.write(f1.read())
    f1.close()
    f2.close()
    print 'done'
copyfile()

9-16

import os


def eightybytes(filename='yan.txt'):
    f = open(filename)
    lines = f.readlines()

    result = []
    for line in lines:
        times = divmod(len(line.strip()), 80)
        start = 0
        stop = 80
        for i in range(times[0] + 1):
            result.append(line.strip()[start:stop] + os.linesep)
            start += 80
            stop += 80
            if times[1] == 0:
                break
    f = open(filename, 'w')
    f.writelines(result)
    f.close()
    print 'done'
eightybytes('yu.txt')


def eightybytes2(filename='yan.txt'):
    f = open(filename)
    lines = f.readlines()

    result = []
    for line in lines:
        if len(line.strip()) <= 80:
            result.append(line)
        else:
            theline = line.strip()
            times = len(theline) / 80
            length = len(theline)
            start = 0
            stop = 80
            for i in range(times + 2):
                while True:
                    if line[stop] in [' ', '.']:
                        result.append(line[start:stop + 1] + os.linesep)
                        break
                    else:
                        stop -= 1
                if stop == length - 1:
                    break
                start = stop + 1
                stop = stop + 80 if stop + 80 < length else length - 1
    f = open(filename, 'w')
    f.writelines(result)
    f.close()
    print 'done'
eightybytes2()

9-17

import os
import os.path

filedic = {}


def creatext(f):
    prompt = '''
(W)rite sth into the file
(Q)uit
Enter your choice: '''
    while True:
        choice = raw_input(prompt)
        if choice.lower() == 'w':
            context = raw_input('write a sentence: ')
            f.write(context + os.linesep)
        else:
            f.seek(0)
            break


def displaycontext():
    choice = whichfile()
    f = filedic[choice]
    for line in f:
        print line,
    f.seek(0)


def editext():
    choice = whichfile()
    f = filedic[choice]
    for i, j in enumerate(f):
        print i, j
    f.seek(0)

    lines = f.readlines()
    f.seek(0)
    chooseindex = input('the index of the line you want to edit: ')
    anewline = raw_input('change it to: ')
    lines[chooseindex] = anewline + os.linesep

    f.writelines(lines)
    f.seek(0)


def savetext():
    choice = whichfile()
    f = filedic[choice]
    lines = f.readlines()

    path = raw_input('the path you want to save the file: ')
    os.chdir(path)
    filename = raw_input("the file's name: ")
    sf = open(os.path.join(path, filename), 'w')

    sf.writelines(lines)
    f.close()
    sf.close()


CMD = {'d': displaycontext, 'e': editext, 's': savetext}


def handletext():
    prompt = '''
welcome to use your own text editor:
(C)reate a text
(D)isplay the context
(E)dit your text
(S)ave the file
(Q)uit
now enter your choice: '''
    n = 0
    while True:

        while True:
            try:
                choice = raw_input(prompt).strip()[0].lower()
            except (EOFError, KeyboardInterrupt):
                choice = 'q'
            print 'your choice: ' + choice

            if choice not in 'cdesq':
                print 'invalid option, try again.'
            else:
                break

        if choice == 'q':
            break
        elif choice == 'c':
            f = os.tmpfile()
            n += 1
            filedic['filename' + str(n)] = f
            print "now this file's name is: " + 'filename' + str(n)
            creatext(f)
        else:
            CMD[choice]()


def whichfile():
    print 'now you have created these file: '
    print filedic.keys()
    choice = raw_input('choose the file you want: ')
    return choice

handletext()

9-18

def searchbyte():
    byte = chr(input('the value of an ASCII byte: '))
    filename = raw_input('the file to search: ')
    f = open(filename)
    num = f.read().count(byte)
    f.close()
    print 'done'
    return num
print searchbyte()

9-19

import random as ran


def binaryfile(chr_number, appear_number, file_length):
    f = open('binaryfile.bin', 'wb')

    printablebyte = range(0x20, 0x7e)
    printablebyte.remove(chr_number)

    writein = [chr(ran.choice(printablebyte)) for i in range(file_length)]

    insertnumlist = range(0, file_length)
    for i in range(appear_number):
        insertnum = ran.choice(insertnumlist)
        insertnumlist.remove(insertnum)
        writein[insertnum] = chr(chr_number)

    f.writelines(writein)
    f.close()
    print 'done'
binaryfile(0x40, 10, 20)


转载于:https://my.oschina.net/u/2519674/blog/668731

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的纺织品企业财务管理系统,源码+数据库+毕业论文+视频演示 在如今社会上,关于信息上面的处理,没有任何一个企业或者个人会忽视,如何让信息急速传递,并且归档储存查询,采用之前的纸张记录模式已经不符合当前使用要求了。所以,对纺织品企业财务信息管理的提升,也为了对纺织品企业财务信息进行更好的维护,纺织品企业财务管理系统的出现就变得水到渠成不可缺少。通过对纺织品企业财务管理系统的开发,不仅仅可以学以致用,让学到的知识变成成果出现,也强化了知识记忆,扩大了知识储备,是提升自我的一种很好的方法。通过具体的开发,对整个软件开发的过程熟练掌握,不论是前期的设计,还是后续的编码测试,都有了很深刻的认知。 纺织品企业财务管理系统通过MySQL数据库与Spring Boot框架进行开发,纺织品企业财务管理系统能够实现对财务人员,员工,收费信息,支出信息,薪资信息,留言信息,报销信息等信息的管理。 通过纺织品企业财务管理系统对相关信息的处理,让信息处理变的更加的系统,更加的规范,这是一个必然的结果。已经处理好的信息,不管是用来查找,还是分析,在效率上都会成倍的提高,让计算机变得更加符合生产需要,变成人们不可缺少的一种信息处理工具,实现了绿色办公,节省社会资源,为环境保护也做了力所能及的贡献。 关键字:纺织品企业财务管理系统,薪资信息,报销信息;SpringBoot
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值