我有一个空格,如包含数据分割的TXT文件:Python的TXT文件转换成JSON剥离和元素订货
2017-05-16 00:44:36.151724381 +43.8187 -104.7669 -004.4 00.6 00.2 00.2 090 C
2017-05-16 00:44:36.246672534 +41.6321 -104.7834 +004.3 00.6 00.3 00.2 130 C
2017-05-16 00:44:36.356132768 +46.4559 -104.5989 -004.2 01.1 00.4 00.2 034 C
,我想将其转换成JSON数据是这样的:
{"dataset": "Lightning","observation_date": "20170516004436151", "location": { "type": "point", "coordinates": [43.8187, -104.7669]}} {"dataset": "Lightning","observation_date": "20170516004436246", "location": { "type": "point", "coordinates": [41.6321, -104.7834]}} {"dataset": "Lightning","observation_date": "20170516004436356", "location": { "type": "point", "coordinates": [46.4559, -104.5989]}}
在那里我有追加一个“数据集”:“闪电”键/值对,结合和剥离的日期和时间,和执行任何JSON转换之前合并纬度/经度为一个字典。
但是现在我仍然获得不被剥夺的日期和时间元素“ - ”和“:”字符,如:
{"observation_date": "2017-05-1600:44:36.151724381", "location": {"type": "point", "coordinates": ["+43.8187", "-104.7669"]}, "dataset": "Lightning"} {"observation_date": "2017-05-1600:44:36.246672534", "location": {"type": "point", "coordinates": ["+41.6321", "-104.7834"]}, "dataset": "Lightning"} {"observation_date": "2017-05-1600:44:36.356132768", "location": {"type": "point", "coordinates": ["+46.4559", "-104.5989"]}, "dataset": "Lightning"}
我编写什么至今:
import json
import sys
def convert(filename):
dataDict = {}
txtFile = filename[0]
print "Opening TXT file: ",txtFile
infile = open(txtFile, "r")
for line in infile:
lineStrip = line.strip()
parts = [p.strip() for p in lineStrip.split()]
date = parts[0].strip("-") #trying to get rid of "-" but not working
time = parts[1].strip(":") #trying to get rid of ":" and "." but not working
dataDict.update({"dataset":"Lightning"})
dataDict.update({"observation_date": date + time})
dataDict.update({"location": {"type":"point", "coordinates": [parts[2], parts[3]]}})
json_filename = txtFile.split(".")[0]+".json"
jsonf = open(json_filename,'a')
data = json.dumps(dataDict)
jsonf.write(data + "\n")
print dataDict
infile.close()
jsonf.close()
if __name__=="__main__":
convert(sys.argv[1:])
但我不知道如何去除“ - ”,“。”和“:”以及在前面放置“数据集”:“闪电”元素。
对'json.dumps()'有一个排序参数,对键进行排序。 'x.replace(' - ','')'从字符串中删除短划线。 – Anthon
谢谢安森!不能相信这是这么简单。 – user2965721
如果其中一个答案解决了您的问题,请通过单击答案旁边的✔(复选标记)来考虑*接受*。这是其他人知道你的问题已经解决的方式,没有阅读评论。它还会在列表中更改问题的外观和此答案。如果有更好的答案出现,您可以随时更改接受的答案。你也可以赞成你认为有价值的所有答案,但你只能接受一个答案。 – Anthon