Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python将所有返回的数据写入JSON文件?_Json_Python 3.x_List_Loops - Fatal编程技术网

如何使用Python将所有返回的数据写入JSON文件?

如何使用Python将所有返回的数据写入JSON文件?,json,python-3.x,list,loops,Json,Python 3.x,List,Loops,如何将返回的dict写入json文件 到目前为止,我能够返回正确的数据并将其打印出来,但当我试图将其写入JSON文件时,它只会写入最后一条记录 如果有人能帮我解决这个问题,我将不胜感激 例如: 打印数据: json文件 代码: 是否要将多个json记录写入单个文件打开(文件名,“w”)每次都会覆盖该文件。使用附加模式“a”。我想要的是每次系统运行时写入多个json记录。但是。。。是否要将它们全部写入一个文件?您每天创建一个唯一的文件名,但由于您以写入模式而不是附加模式打开文件,因此每个记录的文件

如何将返回的dict写入json文件

到目前为止,我能够返回正确的数据并将其打印出来,但当我试图将其写入JSON文件时,它只会写入最后一条记录

如果有人能帮我解决这个问题,我将不胜感激

例如:

打印数据: json文件 代码:
是否要将多个json记录写入单个文件<代码>打开(文件名,“w”)每次都会覆盖该文件。使用附加模式“a”。我想要的是每次系统运行时写入多个json记录。但是。。。是否要将它们全部写入一个文件?您每天创建一个唯一的文件名,但由于您以写入模式而不是附加模式打开文件,因此每个记录的文件都会被覆盖。如果您希望每个记录都有单独的文件,问题在于
filename=“{}\u searchResult.json”。format(今日)
。是的,这是循环。每次以“w”模式打开文件时,都会覆盖其以前的内容。要将所有记录放在一个文件中,您需要将-open(fileName,“a”)作为jsf:
[{"file Name": "test1.txt", "searched Word": "th", "number of occurence": 1}]
[{"file Name": "test2.txt", "searched Word": "th", "number of occurence": 1}]
[{"file Name": "test2.txt", "searched Word": "th", "number of occurence": 1}]
import re
import json
import os.path
import datetime


for counter, myLine in enumerate(textList):
    thematch=re.sub(searchedSTR,RepX,myLine)
    matches = re.findall(searchedSTR, myLine, re.MULTILINE | re.IGNORECASE)


    if len(matches) > 0:  

        # add one record for the match (add one because line numbers start with 1)
        d[matches[0]].append(counter + 1)
    self.textEdit_PDFpreview.insertHtml(str(thematch))
    '''
    loop over the selected file and extract 3 values:
    ==> name of file 
    ==> searched expression
    ==> number of occurence 
    '''
for match, positions in d.items():
    listMetaData = [{"file Name":fileName,"searched Word":match,"number of occurence":len(positions)}]
    jsondata = json.dumps(listMetaData)
    print("in the for loop ==>jsondata: \n{0}".format(jsondata))

    '''
    create a folder called 'search_result' that includes all the result of the searching as JSON file 
    where the system check if the folder exist will continue if not the system create the folder
    insode the folder the file will be created as ==> today_searchResult.js
    '''
if not(os.path.exists("./search_result")):
    try:
        #print(os.mkdir("./search_result"))
        #print(searchResultFoder)

        today = datetime.date.today()
        fileName = "{}_searchResult.json".format(today)
        #fpJ = os.path.join(os.mkdir("./search_result"),fileName)
        #print(fpJ)
        with open(fileName,"w") as jsf:
            jsf.write(jsondata)
            print("finish writing")
    except Exception as e:
        print(e)