2017-07-05 27 views
1

我有一个空格,如包含数据分割的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:]) 

但我不知道如何去除“ - ”,“。”和“:”以及在前面放置“数据集”:“闪电”元素。

+0

对'json.dumps()'有一个排序参数,对键进行排序。 'x.replace(' - ','')'从字符串中删除短划线。 – Anthon

+0

谢谢安森!不能相信这是这么简单。 – user2965721

+0

如果其中一个答案解决了您的问题,请通过单击答案旁边的✔(复选标记)来考虑*接受*。这是其他人知道你的问题已经解决的方式,没有阅读评论。它还会在列表中更改问题的外观和此答案。如果有更好的答案出现,您可以随时更改接受的答案。你也可以赞成你认为有价值的所有答案,但你只能接受一个答案。 – Anthon

回答

1

这应该工作

date = parts[0].replace("-",'') #trying to get rid of "-" but not working 

time = parts[1].replace(":",'') #trying to get rid of ":" and "." but not working 
+0

谢谢!为我工作。 – user2965721

+0

非常欢迎:) – PYA

+0

@ user2965721请接受:) – PYA

1

你应该这样做:

日期=零件[0] .replace( ' - ', '') 时间=零件[1] .replace(': '‘’)

为了让dataset起来JSON面前,你唯一的选择是排序键:

data = json.dumps(dataDict, sort_keys=True) 

你也应该考虑做

dataDict["dataset"] = "Lightning" 

,而不是.update

1

Python词典是无序的,所以你不能指定"dataset":"lightning"元素为第一个。为此,我会使用OrderedDict而不是像其他人提到的那样对json进行排序。

为了正确格式化的时候,我会使用一个datetime对象为这样:

import datetime 

date_string = parts[0] + parts[1] 
format = "%Y-%d-%m%H:%M:%S.%f" 
dt = datetime.strptime(date_string, format) 
new_date_string = dt.strftime("%Y%d%m%H%M%S") 

使用DateTime对象是有帮助的,因为它与大熊猫和numpy的发挥很好,如果你继续在工作除了吐出json之外的数据。如果需要,它还支持数学运算和时区本地化。