我的代码在下面列出。我只是想跟随一个教程。我正在努力最终将其应用到csv格式的长列表中。但是,我似乎无法在不引发错误的情况下运行此简单代码。错误是可怕的FileNotFoundError:[WinError 2]系统找不到指定的文件。任何帮助是极大的赞赏。
import simplekml
import subprocess
import pandas as pd
# names in a list
names = ['test_1',
'test_2',
'test_3']
# lat and longs
latitudes = [47.547921, 48.018745, 47.982146]
longitudes = [-105.125498, -105.325687, -105.6547821]
# piecing together the name, long, and lat values into variable called locations
locations = pd.DataFrame({'names': names,
'longitudes': longitudes,
'latitudes': latitudes})
# creating an instance of the simplekml class
points_kml = simplekml.Kml()
# iterating over the locations variable
for i in locations.itertuples():
points_kml.newpoint(name=i.names, coords=[(i.longitudes, i.latitudes)])
# assigning the variable points_kml_path to where we want to save the file we are creating
points_kml_path = 'c:/Users/rexmo/Documents/points_kml.kml'
points_kml.save(points_kml_path)
# open with Google Earth Pro
#subprocess.call(['open', points_kml_path])
subprocess.run(['open', points_kml_path])
我的回溯错误如下:
runfile('C:/Users/rexmo/Documents/Work/spotter_problems/untitled0.py', wdir='C:/Users/rexmo/Documents/Work/spotter_problems')
Traceback (most recent call last):
File "C:\Users\rexmo\Documents\Work\spotter_problems\untitled0.py", line 31, in <module>
subprocess.run(['open', points_kml_path])
File "C:\Users\rexmo\anaconda3\lib\subprocess.py", line 488, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\rexmo\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 104, in __init__
super(SubprocessPopen, self).__init__(*args, **kwargs)
File "C:\Users\rexmo\anaconda3\lib\subprocess.py", line 800, in __init__
restore_signals, start_new_session)
File "C:\Users\rexmo\anaconda3\lib\subprocess.py", line 1207, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
文件正在被写入代码中列出的目录。我可以双击文件夹中的文件,然后按预期在Google Earth中将其打开。我不知所措。
您可以尝试使用pathlib吗?
from pathlib import Path
# Replace this line
points_kml_path = 'c:/Users/rexmo/Documents/points_kml.kml'
# With this
points_kml_path = Path.home() / 'Documents' / 'points_kml.kml'
# Edit, I don't know if you might need it as a string
points_kml_path = Path.home() / 'Documents' / 'points_kml.kml'
points_kml_path = str(points_kml_path)
因此,我找到了两种获取所需结果的不同方法,这只是使用上述脚本中创建的点来启动Google Earth。第一个,似乎不太复杂的是
import os
os.startfile(points_kml_path)
另一个看似不太复杂的方法是
# Path to Google Earth exe
earth = 'c:\Program Files\Google\Google Earth Pro\client\googleearth.exe'
subprocess.run([earth, points_kml_path])
似乎很奇怪,我需要明确显示Google Earth可执行文件的完整路径。如果还有其他方法,我仍然会对听到它感兴趣。