练习题4

  1. 现有一个文件test.txt ,内容以下:

1234efghpython

abcd5678git

要求读出文件内容,对内容的顺序进行编辑,而后从新写入到文件,使其为以下形式ide

12345678调试

abcdefghcode

注意事项:使用pycharm的同窗在调试程序时,若是程序对文件进行了操做,而后手动修改了文件,则要在pycharm中,程序所在的目录上点击右键,选择clean python compiled files,不然可能会报错orm

  1. 将上周五生成的dict3,排序后写入到文件dict.txt中,要求格式为

A 65排序

B 66ip

C 67utf-8

...pycharm

x 120

y 121

z 122

一、

#!/usr/bin/env python

file = open('/root/python/test.txt')

fd = file.readlines()

file.close

num = (fd[0].strip() + fd[1].strip())

a = sorted(list(filter(str.isalpha, num)))

b = sorted(list(filter(str.isdigit, num)))

print(''.join(a))

print(''.join(b))

二、将上周五生成的dict3,排序后写入到文件dict.txt中

#!/usr/bin/env python

-- coding: utf-8 --

@Time : 2017-12-29 14:22

@Author : anChow

@File : dict3排序.py

dict3 = sorted(dict3.items(), key=lambda x: x[1])

with open('dict.txt', 'w') as fd:

for i in dict3:

    a = i[0]

    b = str(i[1])

    fd.write("{0}    {1}\n".format(a, b))
相关文章
相关标签/搜索