Python在同一个函数中读写

时间:2016-02-01 15:41:03

标签: python-2.7 csv

我的代码目前正在接收csv文件并输出到文本文件。我下面的代码片段和我遇到的问题是来自csv我正在搜索像问题这样的关键字,并且每行都有我要输出的那个字到文本文件中。目前,我将它打印到一个JSON文件,但它全部在这一行上 “什么,something1,something2,something3,something4,something5,something6,something7 \ r \ n” 个 “的东西,something1,something2,something3,something4,something5,something6,something7 \ r \ n” 个

但是我希望它打印出来像这样:

“的东西,something1,something2,something3,something4,something5,something6,something7” “什么,something1,something2,something3,something4,something5,something6,something7”

这是我到目前为止的代码:

def search(self, filename):
        with open(filename, 'rb') as searchfile, open("weekly_test.txt", 'w') as text_file:
            for line in searchfile:
                if 'PBI 43125' in line:
                    #print (line)
                    json.dump(line, text_file, sort_keys=True, indent = 4)

所以我再次需要一些关于如何让我的json文件按照我想要的方式进行格式化的指导。

1 个答案:

答案 0 :(得分:1)

只需将$data = array( 'method' => "create_video", 'params' => array( 'video' => array( 'name' => $video->filename, 'shortDescription' => 'Sample video' ), "token" => $this->config->item('brightcove_write_token'), "encode_to" => "MP4", "create_multiple_renditions" => "True" ), ); $data_string = json_encode($data); $url = 'http://api.brightcove.com/services/post'; $fields = array( 'json' => $data_string, 'file' => new CURLFile(FCPATH.'assets/uploads/submitted/'.$video->filename) ); //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //execute post $result = json_decode(curl_exec($ch)); 替换为print line

即可
print >>file, line