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

1234efgh

abcd5678

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

12345678

abcdefgh

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

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

A 65

B 66

C 67

...

x 120

y 121

z 122

1、

#!/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))

2、将上周五生成的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))