一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python實現按特定格式對文件進行讀寫的方法示例

Python實現按特定格式對文件進行讀寫的方法示例

2020-12-21 00:28愛橙子的OK繃 Python

這篇文章主要介紹了Python實現按特定格式對文件進行讀寫的方法,可實現文件按原有格式讀取與寫入的功能,涉及文件的讀取、遍歷、轉換、寫入等相關操作技巧,需要的朋友可以參考下

本文實例講述了Python實現按特定格式對文件進行讀寫的方法。分享給大家供大家參考,具體如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /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實現按特定格式對文件進行讀寫的方法示例

每一行的數據之間其實已經加入空格。

運行結果:

?
1
2
3
4
5
6
7
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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产专区一va亚洲v天堂 | 亚洲欧美乱| 国产精品久久久久a影院 | 精品视频手机在线观看免费 | 亚洲天堂网在线观看视频 | 免费亚洲视频 | 免费国产网站 | 暖暖 免费 高清 日本 在线1 | 黄a级| 千金肉奴隶在线观看 | 日韩高清在线免费观看 | 欧美在线一级视频 | 滑进了柔佳火热紧夹的 | 日本老妇和子乱视频 | sss亚洲国产欧美一区二区 | 国产午夜精品一区二区三区不卡 | 亚洲第一永久色 | 无套暴躁白丝秘书 | 国外欧美一区另类中文字幕 | 九九九九在线精品免费视频 | 久久re这里精品在线视频7 | 成人精品一区久久久久 | 水野朝阳厨房系列在线观看 | 四虎官网| 免费刷10000名片赞网站 | 99精品在线免费观看 | 免费亚洲一区 | 久久一er精这里有精品 | 风间由美m3u8在线 | 6080欧美一区二区三区四区 | caoporm碰最新免费公开视频 | 青草视频免费 | 性欧美金发洋妞xxxxbbbb | 成人午夜在线视频 | 成人在线第一页 | 91制片厂免费观看 | 日本免费三片在线观看 | 97影院手机版 | 久久精品热在线观看30 | 丝瓜草莓香蕉绿巨人幸福宝 | 亚洲色图欧美偷拍 |