大家好,我一直在调整一个让我感到不快的错误,因为我已经有了类似的代码而且没有任何问题。我想这是因为ast.literal_eval但是在我的其他Geolocation代码中它运行正常,唯一的区别是我在我的工作代码中有8个协调而不是2个。
from __future__ import print_function
from shapely.wkt import loads as load_wkt
from shapely.geometry import Point, Polygon
import re
import ast
import io
with io.open('sample_test1.txt', encoding="utf-8") as f, io.open('sample_test2.txt.txt', 'w',encoding="utf-8") as g:
for line in f:
j = re.sub(r'.*(\[.*\]).*', r'\1', line.rstrip())
k = ast.literal_eval(j)
p = Polygon(k)
c = p.centroid
print (c, file = g)
f.close()
g.close()
re.sub捕获的数据如下所示:
[(38.78866,-75.56358),(41.357426,-75.56358),(41.357426,-73.88506),(38.78866,-73.88506)]
这里是原始输入:
703971723738288128,[(39.87181,-75.28028),(40.13792,-75.28028),(40.13792,-74.95571),(39.87181,-74.95571)],,,lmao maybe but I probably listened to TLOP almost everyday so I heard everyone song at least 10 times ðŸ˜
703971724577148928,[(37.88661,-79.48765),(39.72362,-79.48765),(39.72362,-74.98628),(37.88661,-74.98628)],,,Whose trying to come out and enjoy this beautiful weather?
703971724635852800,[(39.87181,-75.28028),(40.13792,-75.28028),(40.13792,-74.95571),(39.87181,-74.95571)],21886583,WorkFromHomeQ102Meet5H,I just got home from work now I'm listening to #WorkFromHome 😜 #Q102Meet5H @Q102Philly
我一直都在犯这个错误:
File "polygon_csv.py", line 30, in <module>
k = ast.literal_eval(j)
File "...\Anaconda3\lib\ast.py", line 84, in literal_eval
return _convert(node_or_string)
File "...\Anaconda3\lib\ast.py", line 55, in _convert
return tuple(map(_convert, node.elts))
File "...\Anaconda3\lib\ast.py", line 83, in _convert
raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Name object at 0x00000203D2EA0F98>