python对文本文档的读写和删除操作-2

本文档介绍如何使用Python处理JSON格式的数据。项目中涉及到读取特定目录下的JSON文件,提取calls数据并返回,供RobotFramework使用。在后续测试用例中,这些calls数据结合时间间隔会被写入新的文件。
摘要由CSDN通过智能技术生成

项目需要,首先需要读取json格式存储的文本文档,该文本文档存在指定的目录中,该目录下有很多的文本文档,需要在文本文档中找到里面的calls数据,然后把calls数据返回,供robotframework 调用,然后在另一个测试用例中将calls数据和时间间隔写入另一个文本文档中。

#-*- coding:utf-8 -*-

import os

class Write_in_Txt(object):
    def __init__(self):
        self._write_in_file = None

    def get_written_data(self, calls1, calls2):
        dict_call = {"calls_before" :calls1, "calls_after" :calls2}
        return dict_call

    def write_to_txt_file(self, file_path, dict_call, time_interval):
        if not os.path.exists(file_path):
            file_name = open(file_path, 'w')
            for key,value in dict_call.items():
                print 'key',key
                print 'value',value
                file_name.write(key + ":" + value + '---')
            file_name.write('time_interval:' + time_interval)
            file_name.write('\n')
            file_name.close()
        else:
            file_name = open(file_path, 'a')
            for key,value in dict_call.items():
                 print 'key',key
                 print 'value',value
                 file_name.write(key + ":" + value + '---')
            file_name.write('time_interval:' + time_interval)
            file_name.write('\n')
            file_name.close()

if __name__ == '__main__':
    current_dir = os.getcwd()
    print 'current_dir', current_dir
    file_path = current_dir +"\\" + "\\calls_collection.txt"
    write_in_file = Write_in_Txt()
    dict_data = write_in_file.get_written_data("10","11")
    write_in_file.write_to_txt_file(file_path, dict_data, "2016-08-01~2016-08-10")
    dict_data2 = write_in_file.get_written_data("5","6")
    write_in_file.write_to_txt_file(file_path, dict_data, "2016-05-01~2016-05-10")



评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值