Python 3.7.3 Windows 10.0 Jupyter 1.0.0
我有一个OrderedDict对象列表。序列化有效
st=json.dumps(js)
print(st)
>>>'[{"rank": 0, "url": "https://www.almanac.com/plant/brussels-sprouts", "title": "Brussels Sprouts: Planting, Growing, and Harvesting ...", "snippet": "Planting Brussels Sprouts. Due to Brussels sprouts\\u2019 need for a long growing season, plant them with a fall or winter harvest in mind. In regions with cold winters, where temperatures are regularly below freezing, start seeds indoors about 2\\u20133 weeks before the last spring frost date for an early fall harvest."}, {"rank": 1, "url": "https://www.thespruce.com/growing-brussels-sprouts-1403442", "title": "How to grow Brussels Sprouts Plants - The Spruce", "snippet": "As with broccoli, growing Brussels sprouts in warm weather and long days will cause the \\u201csprouts\\u201d to open and be unsuitable for eating. Named after the city of Brussels, Brussels sprouts were first made popular in Belgium, where they\\u2019ve been grown since about 1200."}]'
但是当我尝试写入文件时,文件已创建但仍然为空
outfile=open('SE_ BING_{}_2016999999.json'.format(queryN),'w')
outfile.write(st)
编写普通字符串可以正常工作:
outfile.write('Hello World')
编辑:由于神秘原因,问题得以解决,方法是更正了代码的其他部分