Python实现按特定格式对文件进行读写的方法示例

作者:爱橙子的OK绷 时间:2022-06-07 22:59:57 

本文实例讲述了Python实现按特定格式对文件进行读写的方法。分享给大家供大家参考,具体如下:


#! /usr/bin/env python
#coding=utf-8
class ResultFile(object):
 def __init__(self, res):
   self.res = res
 def WriteFile(self):
   fp = open('pre_result.txt', 'w')
   print 'write start!'
   try:
     for item in self.res:
       fp.write(item['host'])
       fp.write('\r')
       fp.write(str(item['cpu']))#write方法的实参需要为string类型
       fp.write('\r')
       fp.write(str(item['mem']))
       fp.write('\n')
   finally:
     fp.close()
     print 'write finish!'
 def ReadFile(self):
   res = []
   fp = open('pre_result.txt', 'r')
   try:
     lines = fp.readlines()#读取出全部数据,按行存储
   finally:
     fp.close()
   for line in lines:
     dict = {}
     #print line.split() #like['compute21', '2', '4']
     line_list = line.split() #默认以空格为分隔符对字符串进行切片
     dict['host'] = line_list[0]
     dict['cpu'] = int(line_list[1])#读取出来的是字符
     dict['mem'] = int(line_list[2])
     res.append(dict)
   return res
if __name__ == '__main__':
 result_list=[{'host':'compute21', 'cpu':2, 'mem':4},{'host':'compute21', 'cpu':2, 'mem':4},
        {'host':'compute22', 'cpu':2, 'mem':4},{'host':'compute23', 'cpu':2, 'mem':4},
        {'host':'compute22', 'cpu':2, 'mem':4},{'host':'compute23', 'cpu':2, 'mem':4},
        {'host':'compute24', 'cpu':2, 'mem':4}]
 file_handle = ResultFile(result_list)
 #1、写入数据
 #print 'write start!'
 file_handle.WriteFile()
 #print 'write finish!'
 #2、读取数据
 res = file_handle.ReadFile()
 print res

写入的文件:

Python实现按特定格式对文件进行读写的方法示例

每一行的数据之间其实已经加入空格。

运行结果:


write start!
write finish!
[{'mem': 4, 'host': 'compute21', 'cpu': 2}, {'mem': 4, 'host':
'compute21', 'cpu': 2}, {'mem': 4, 'host': 'compute22', 'cpu': 2},
{'mem': 4, 'host': 'compute23', 'cpu': 2}, {'mem': 4, 'host':
'compute22', 'cpu': 2}, {'mem': 4, 'host': 'compute23', 'cpu': 2},
{'mem': 4, 'host': 'compute24', 'cpu': 2}]

实现了按原有格式写入和读取。

希望本文所述对大家Python程序设计有所帮助。

来源:http://blog.csdn.net/will130/article/details/50478481

标签:Python,文件读写
0
投稿

猜你喜欢

  • python中文乱码的解决方法

    2022-07-12 18:03:48
  • 关于python中map函数的使用

    2022-10-30 22:43:16
  • 解析smarty模板中类似for的功能实现

    2023-11-15 12:53:40
  • Python如何筛选序列中的元素的方法实现

    2021-06-12 03:31:51
  • Python利用matplotlib.pyplot绘图时如何设置坐标轴刻度

    2023-09-18 23:32:01
  • python实现图片变亮或者变暗的方法

    2023-02-20 03:59:00
  • python构造IP报文实例

    2023-07-10 20:40:51
  • 如何用拦截表单的方法上传图片?

    2010-06-16 09:50:00
  • Python Selenium XPath根据文本内容查找元素的方法

    2022-02-18 12:55:16
  • PHP addslashes()函数讲解

    2023-06-04 04:28:24
  • 微信小程序地图定位的实现方法实例

    2023-08-25 10:13:10
  • python实现彩票系统

    2021-04-12 21:16:42
  • 查看ASP详细错误提示信息的图文设置方法

    2011-02-05 11:02:00
  • PHP session会话的安全性分析

    2023-11-21 23:47:59
  • 微信小程序 云开发模糊查询实现解析

    2023-08-24 14:47:57
  • 基于Pyinstaller打包Python程序并压缩文件大小

    2023-11-10 06:41:05
  • Php中用PDO查询Mysql来避免SQL注入风险的方法

    2023-07-18 06:37:42
  • 完美的js验证网址url(正则表达式)

    2008-06-07 09:36:00
  • Asp WinHttp.WinHttpRequest.5.1 对象使用详解

    2012-05-02 10:15:27
  • Python tkinter 树形列表控件(Treeview)的使用方法

    2023-10-06 23:14:47
  • asp之家 网络编程 m.aspxhome.com