来自 csv 的多段线 - 不同于字符串文字的行为
Polylines from csv - different behaviour than from string literal
我阅读了很多关于编码的 SO 主题以了解我的代码有什么问题,但我仍然卡住了。
我想解码 csv 文件中的 Google 多段线。我正在使用工作正常的折线库。问题是,当我从 csv 读取它们时,无法处理来自 csv 的某些行,但是当我只是将该多段线作为字符串文字传递时,它工作正常。我认为这是一些编码问题,因为造成问题的折线有两个连续的反斜杠 and/or 反引号。
import csv
import polyline
INPUT_FILE = 'sample_input.csv'
csv.register_dialect(
'mydialect',
delimiter = ',',
quotechar = '"',
doublequote = True,
skipinitialspace = True,
quoting = csv.QUOTE_ALL)
with open(INPUT_FILE, 'r', encoding="utf-8") as csv_file:
read = csv.reader(csv_file, dialect='mydialect')
header = next(read, [])
for row in read:
site_id = row[0]
encoded_polyline = row[1]
print(site_id)
try:
decoded = polyline.decode(encoded_polyline)
print(decoded)
except:
print(encoded_polyline)
print()
示例折线为:
"dk`mEg}jx[STEFGJKRONUVSTkAtAiAlAsA~Ag@p@[^[`@e@p@KTSVU\GHGNEHEHCFAFAFAFAPAP?N?B@T@V@R@F"
请注意这里也只有一个反斜杠没有反引号 - 可能是类似的编码问题?
任何帮助将不胜感激,特别是解释为什么字符串文字的行为与字符串变量的行为不同。
这应该可以解决您的问题
decoded = polyline.decode(encoded_polyline.replace('\\','\'))
我遇到了类似的问题,然后意识到折线包含高程字段,因此如果你想获得 x、y、z,你应该使用参数 is3d=True。
decoded = polyline.decode(encoded_polyline,is3d=True)
我阅读了很多关于编码的 SO 主题以了解我的代码有什么问题,但我仍然卡住了。
我想解码 csv 文件中的 Google 多段线。我正在使用工作正常的折线库。问题是,当我从 csv 读取它们时,无法处理来自 csv 的某些行,但是当我只是将该多段线作为字符串文字传递时,它工作正常。我认为这是一些编码问题,因为造成问题的折线有两个连续的反斜杠 and/or 反引号。
import csv
import polyline
INPUT_FILE = 'sample_input.csv'
csv.register_dialect(
'mydialect',
delimiter = ',',
quotechar = '"',
doublequote = True,
skipinitialspace = True,
quoting = csv.QUOTE_ALL)
with open(INPUT_FILE, 'r', encoding="utf-8") as csv_file:
read = csv.reader(csv_file, dialect='mydialect')
header = next(read, [])
for row in read:
site_id = row[0]
encoded_polyline = row[1]
print(site_id)
try:
decoded = polyline.decode(encoded_polyline)
print(decoded)
except:
print(encoded_polyline)
print()
示例折线为:
"dk`mEg}jx[STEFGJKRONUVSTkAtAiAlAsA~Ag@p@[^[`@e@p@KTSVU\GHGNEHEHCFAFAFAFAPAP?N?B@T@V@R@F"
请注意这里也只有一个反斜杠没有反引号 - 可能是类似的编码问题?
任何帮助将不胜感激,特别是解释为什么字符串文字的行为与字符串变量的行为不同。
这应该可以解决您的问题
decoded = polyline.decode(encoded_polyline.replace('\\','\'))
我遇到了类似的问题,然后意识到折线包含高程字段,因此如果你想获得 x、y、z,你应该使用参数 is3d=True。
decoded = polyline.decode(encoded_polyline,is3d=True)