练习题4

  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))

转载于:https://blog.51cto.com/goldstar52/2136288

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值