我有一个文本文件,想要将数据格式更改为GeoJson格式。首先,我想将它解析为分离的部分。我无法使用python来创建代码。请帮助我解析数据并制作GeoJson格式。初始数据格式如下所示。
.A DAQT2 170314 C DH124045 /HGIRS 479.7:
.A DAMT2 170314 C DH115756 /HGIRS 425.1:
.A DBQT2 170314 C DH123840 /HGIRS 436.6:
.A DBDT2 170314 C DH120419 /HGIRS 472.3:
.A DDDT2 170314 C DH120204 /HGIRS 400.6:
.A DCNT2 170314 C DH120806 /HGIRS 412.9:
.A DBUT2 170314 C DH124633 /HGIRS 404.1:
.A DBBT2 170314 C DH121511 /HGIRS 453.4:
.A DDNT2 170314 C DH123336 /HGIRS 0.2:
.A DAXT2 170314 C DH123031 /HGIRS 438.1:
我生成了以下代码,直到现在首先分离数据(解析它们),如下所示,但它返回一行数据。我希望输出逐行。
file = open('test1.txt', 'r')
lines = file.readlines()
length=lines[0].split('\n')
number_of_lines=len(length)
lines=str(lines)
data=lines.split(' ')
data=str(data)
data=data.split('\n')
file.close()
file = open('test1.txt', 'w')
file.write(str(data))
file.write('\n')
file.close()
file = open('test1.txt', 'w')
file.write(str(data))
file.write('\n')
file.close()
输出如下:
['["[\':", \'1117\', \'AM\', \'CDT\', \'TUE\', \'MAR\', \'14\',
"2017\\\\n\',", "\':\\\\n\',", "\':", \'HOURLY\', \'ACCUMULATOR\',
\'INFORMATION\', "TABLE\\\\n\',", "\':\\\\n\',", "\':", \'NOTE:\', \'\',
\'ERRONEOUS\', \'REPORTS\', \'MAY\', \'BE\', \'RECEIVED\', \'UNDER\',
"CERTAIN\\\\n\',", "\':", \'\', \'\', \'\', \'\', \'\', \'\', \'\',
\'WEATHER\', "CONDITIONS\\\\n\',", "\':\\\\n\',", "\':",
"**********************************************************\\\\n\',", "\':",
\'ID\', \'\', \'\', \'\', \'LOCATION\', \'\', \'\', \'\', \'\', \'\', \'\',
\'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
\'ACCUMULATOR\', "VALUE\\\\n\',", "\':",
"**********************************************************\\\\n\',",
"\':CITY", \'OF\', \'DALLAS\', \'ALERT\', "SYSTEM\\\\n\',", "\'.A",
\'DDFT2\', \'170314\', \'C\', \'DH103231\', \'/HGIRS\', "516.8:\\\\n\',",
"\'.A", \'DCVT2\', \'170314\', \'C\', \'DH110026\', \'/HGIRS\',
"536.0:\\\\n\',", "\'.A", \'DDJT2\', \'170314\', \'C\', \'DH102056\',
\'/HGIRS\', "0.0:\\\\n\',", "\'.A", \'DDUT2\', \'170314\', \'C\',
\'DH100503\', \'/HGIRS\', "3.6:\\\\n\',", "\'.A", \'DDUT2\', \'170314\',
\'C\', \'DH100533\', \'/HGIRS\', "3.9:\\\\n\',", "\'.A", \'DDUT2\',
\'170314\', \'C\', \'DH100603\', \'/HGIRS\', "4.0:\\\\n\',", "\'.A",
\'DDUT2\', \'170314\', \'C\', \'DH100703\', \'/HGIRS\', "4.2:\\\\n\',",
"\'.A", \'DDUT2\', \'170314\', \'C\', \'DH101603\', \'/HGIRS\',
"4.5:\\\\n\',", "\'.A", \'DDUT2\', \'170314\', \'C\', \'DH103404\',
\'/HGIRS\', "4.7:\\\\n\',", "\'.A", \'DDWT2\', \'170314\', \'C\',
\'DH100740\', \'/HGIRS\', "0.0:\\\\n\',", "\'.A", \'DCXT2\', \'170314\',
\'C\', \'DH105825\', \'/HGIRS\', "614.1:\\\\n\',", "\'.A", \'DCRT2\',
\'170314\', \'C\', \'DH101815\', \'/HGIRS\', "381.1:\\\\n\',", "\'.A",
\'DBGT2\', \'170314\', \'C\', \'DH103921\', \'/HGIRS\', "394.2:\\\\n\',",
"\'.A", \'DBUT2\', \'170314\', \'C\', \'DH105533\', \'/HGIRS\',
"404.1:\\\\n\',", "\'.A", \'DBRT2\', \'170314\', \'C\', \'DH101936\',
\'/HGIRS\', "447.6:\\\\n\',", "\'.A", \'DBRT2\', \'170314\', \'C\',
\'DH103306\', \'/HGIRS\', "447.6:\\\\n\',", "\'.A", \'DBZT2\', \'170314\',
\'C\', \'DH102317\', \'/HGIRS\', "397.1:\\\\n\',", "\'.A", \'DATT2\',
\'170314\', \'C\', \'DH102058\', \'/HGIRS\', "448.6:\\\\n\']"]']
答案 0 :(得分:1)
如果你分开('')" Hello world / n嗨,某人/ n"输出将是
['Hello','world/n','Hi','someone/n']
然后,如果你使用str转换它,结果将是
"['Hello','world/n','Hi','someone/n']"
解决方案是在第一个数组的每个元素中执行for
。
data = lines.split('/n')
data[:] = [x.split(' ') for x in data]
这样数据的输出将是:
[['Hello', 'world'], ['Hi','someone']]
然后,您可以使用for
逐行编写该数据。
对于这个结果:
Hello
world
Hi
someone
只需:
for line in data:
for word in line:
file.write("%s/n" % word)
如果您想要的输出是:
Hello world
Hi someone
然后:
for line in data:
for word in line:
file.write(word)
file.write('/n')
提示, 您还可以将数据加入数组:
>>>a = ["This","is","a","sentence"]
>>>"/n".join(a)
This
is
a
sentence
但是如果你试试这个会给你一个错误:
>>>a = [["This","is","a","sentence"],["An","Other","sentence"]]
>>>"/n".join(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, list found
但你可以尝试:
>>>a = [["This","is","a","sentence"],["An","Other","sentence"]]
>>>a[:] = [' '.join(x) for x in a]
>>>a
['This is a sentence', 'An Other sentence']