我创建dic类型值,这是我的代码
'
' get the columns of range Rr
' Cols as CSV like 2,4,7 b,d,G B,D:F,m,P 3,H,J,2
' 3,4,5,6 is C:F 6,8:11,3 is bad => 6,8,3 B,D:F is 2,4,5,6
'
Function GetRaColl(Rr As Range, ColS$) As Range
Dim SA$(), LI&, VV&, UB&, URa As Range
SA = Split(ColS, ",")
UB = UBound(SA)
VV = Val(SA(0))
If VV > 0 Then
Set URa = Rr.Columns(VV)
Else
Set URa = Rr.Columns(SA(LI))
End If
' if more ranges
For LI = 1 To UBound(SA)
VV = Val(SA(LI))
If VV > 0 Then ' so treats 3:5 as 3 so use C:E or 3,4,5
Set URa = Union(URa, Rr.Columns(VV))
Else
Set URa = Union(URa, Rr.Columns(SA(LI)))
End If
Next LI
Set GetRaColl = URa
End Function
当我打印该值时,它可以正常工作,但是f。写这个“ dic”值,每个元组上都没有\ n
这是打印结果(dic)
date = ["2015 1 3", "2015 1 3", "2015 1 3"]
des = ["btcarchitect coinkite blockchain circlebits...", "swiftstealth offers you privacy in bitswift v2 ...", "all in one article bitcoin blockchain ..."]
for i in range(3) :
dic = {"date":date[i], "description":des[i]}
with open('j.json', 'a', encoding='UTF-8-sig') as f:
f.write(json.dumps(dic, indent=2, ensure_ascii=False))
print (json.dumps(dic, indent=2, ensure_ascii=False) )
我的json文件就是这样保存的,
{
"date": "2015 1 3",
"description": "btcarchitect coinkite blockchain circlebits..."
}
{
"date": "2015 1 3",
"description": "swiftstealth offers you privacy in bitswift v2 ..."
}
{
"date": "2015 1 3",
"description": "all in one article bitcoin blockchain ..."
}
我如何保存}和\ n {?
答案 0 :(得分:1)
您可以简单地在每行末尾添加\n
。这是写方法:
outputFile=open('somefile.txt','a')
for i in range(3) :
dic = {"date":date[i], "description":des[i]}
outputFile.write(json.dumps(dic) + '\n')
outputFile.close()
答案 1 :(得分:1)
import json
date = ["2015 1 3", "2015 1 3", "2015 1 3"]
des = ["btcarchitect coinkite blockchain circlebits...", "swiftstealth offers you privacy in bitswift v2 ...", "all in one article bitcoin blockchain ..."]
dicts = []
for i in range(3) :
dic = {"date":date[i], "description":des[i]}
dicts.append(dic)
file=open('json.txt','a')
for i in dicts:
file.write(json.dumps(i))
file.write('\n')
file.close()
答案 2 :(得分:0)
尝试以下方法。与psychoCoder类似的想法。 -
import json
date = ["2015 1 3", "2015 1 3", "2015 1 3"]
des = ["btcarchitect coinkite blockchain circlebits...", "swiftstealth offers you privacy in bitswift v2 ...", "all in one article bitcoin blockchain ..."]
with open('j.json', 'a') as f:
for i in range(3) :
dic = {"date":date[i], "description":des[i]}
f.write(json.dumps(dic, indent=2, ensure_ascii=False))
f.write("\n")
print (json.dumps(dic, indent=2, ensure_ascii=False) )