遍历所有 csv 文件的目录并为每个单独的 (csv) 文件创建 kml 并使用文件名保存
Iterating through directory for all csv files and creating kml for each individual(csv) file and save using file name
import csv
import simplekml
import pandas as pd
import glob
frame = pd.DataFrame()
filelist=glob.glob('/Users/germanportes/Documents/Status_Report/Telework_training/Anomaly_6/files/*.csv')
kml = simplekml.Kml()
for file in filelist:
a6 =pd.read_csv(file)
for row in a6:
kml.newpoint(name=a6['idfa'], description = a6['device_os'],coords = [(a6['longitude'], a6['latitude'])])
kml.save('/Users/germanportes/Documents/Status_Report/Telework_training/Anomaly_6/files/kml/'+str(a6)+'.csv')
我喜欢使用文件名
将每个单独的 csv 保存为自己的 kml
在这里,您将遍历列而不是行,然后将 pandas.Series
作为列传递给 kml.newpoint
参数而不是某些值。使用 DataFrame.apply() 遍历数据帧行并将每行的一个点添加到您的 kml 对象,如下所示:
from os.path import join
from glob import iglob
from pathlib import Path
import simplekml
import pandas as pd
csv_dir = 'path/to/csv/directory'
kml_dir = 'path/to/kml/directory'
for file in iglob(join(csv_dir, '*.csv')):
# read the csv file
df = pd.read_csv(file)
# make an empty kml object
kml = simplekml.Kml()
# iterate over the rows and and add new points to kml
df.apply(lambda x: kml.newpoint(name=x['idfa'], description = x['device_os'], coords=[(x['longitude'], x['latitude'])]), axis=1)
# save it as kml with the csv filename
kml.save(join(kml_dir, '{}.kml'.format(Path(file).stem)))
import csv
import simplekml
import pandas as pd
import glob
frame = pd.DataFrame()
filelist=glob.glob('/Users/germanportes/Documents/Status_Report/Telework_training/Anomaly_6/files/*.csv')
kml = simplekml.Kml()
for file in filelist:
a6 =pd.read_csv(file)
for row in a6:
kml.newpoint(name=a6['idfa'], description = a6['device_os'],coords = [(a6['longitude'], a6['latitude'])])
kml.save('/Users/germanportes/Documents/Status_Report/Telework_training/Anomaly_6/files/kml/'+str(a6)+'.csv')
我喜欢使用文件名
将每个单独的 csv 保存为自己的 kml在这里,您将遍历列而不是行,然后将 pandas.Series
作为列传递给 kml.newpoint
参数而不是某些值。使用 DataFrame.apply() 遍历数据帧行并将每行的一个点添加到您的 kml 对象,如下所示:
from os.path import join
from glob import iglob
from pathlib import Path
import simplekml
import pandas as pd
csv_dir = 'path/to/csv/directory'
kml_dir = 'path/to/kml/directory'
for file in iglob(join(csv_dir, '*.csv')):
# read the csv file
df = pd.read_csv(file)
# make an empty kml object
kml = simplekml.Kml()
# iterate over the rows and and add new points to kml
df.apply(lambda x: kml.newpoint(name=x['idfa'], description = x['device_os'], coords=[(x['longitude'], x['latitude'])]), axis=1)
# save it as kml with the csv filename
kml.save(join(kml_dir, '{}.kml'.format(Path(file).stem)))