我在使用Python将XML文件转换为CSV时遇到了很多麻烦.我看了很多论坛,尝试了lxml和xmlutils.xml2csv,但我无法让它工作.这是来自Garmin GPS设备的GPS数据.
这是我的XML文件的样子,当然缩短了:
<?xml version="1.0" encoding="utf-8"?>
<gpx xmlns:tc2="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tp1="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="TC2 to GPX11 XSLT stylesheet" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd">
<trk>
<name>2013-12-03T21:08:56Z</name>
<trkseg>
<trkpt lat="45.4852855" lon="-122.6347885">
<ele>0.0000000</ele>
<time>2013-12-03T21:08:56Z</time>
</trkpt>
<trkpt lat="45.4852961" lon="-122.6347926">
<ele>0.0000000</ele>
<time>2013-12-03T21:09:00Z</time>
</trkpt>
<trkpt lat="45.4852982" lon="-122.6347897">
<ele>0.2000000</ele>
<time>2013-12-03T21:09:01Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>
Run Code Online (Sandbox Code Playgroud)
我的大量XML文件中有几个trk标签,但我可以设法将它们分开 - 它们代表不同的"段"或GPS设备上的行程.我想要的只是一个CSV文件,其中包含以下内容:
LAT LON TIME ELE
45.4... -122.6... 2013-12... 0.00...
... ... ... ...
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的代码:
## Call libraries
import csv
from xmlutils.xml2csv import xml2csv
inputs = "myfile.xml"
output = "myfile.csv"
converter = xml2csv(inputs, output) …Run Code Online (Sandbox Code Playgroud) 当我按某些属性对我的数据进行分组时,我想添加一个“总计”行来提供比较基线。让我们按汽缸和化油器对 mtcars 进行分组,例如:
by_cyl_carb <- mtcars %>%
group_by(cyl, carb) %>%
summarize(median_mpg = median(mpg),
avg_mpg = mean(mpg),
count = n())
Run Code Online (Sandbox Code Playgroud)
...产生这些结果:
> by_cyl_carb
# A tibble: 9 x 5
# Groups: cyl [?]
cyl carb median_mpg avg_mpg count
<dbl> <dbl> <dbl> <dbl> <int>
1 4 1 27.3 27.6 5
2 4 2 25.2 25.9 6
3 6 1 19.8 19.8 2
4 6 4 20.1 19.8 4
5 6 6 19.7 19.7 1
6 8 2 17.1 17.2 4
7 8 …Run Code Online (Sandbox Code Playgroud) 使用dplyr中的包R,我想将过滤器语句作为函数中的参数传递。我不知道如何将语句评估为代码而不是字符串。当我尝试下面的代码时,我收到一条错误消息。我假设我需要一个 quosure 或其他东西,但我没有完全理解这个概念。
data("PlantGrowth")
myfunc <- function(df, filter_statement) {
df %>%
filter(!!filter_statement)
}
myfunc(PlantGrowth, "group %in% c('trt1', 'trt2')")
> Error: Argument 2 filter condition does not evaluate to a logical vector
# Want to do the same as this:
# PlantGrowth %>%
# filter(group %in% c('trt1', 'trt2'))
Run Code Online (Sandbox Code Playgroud)