d-_*_*_-b -1 bash awk python csv
这显然是一个流行的面试问题:
有 2 个包含恐龙数据的 CSV 文件。我们需要查询它们以返回满足特定条件的恐龙。
有 2 个选择 - 仅使用 Unix 命令行工具 ( cut
/// ) ,或者使用 Python 等脚本语言,但不使用、等附加paste
模块。sed
awk
q
fsql
csvkit
NAME,LEG_LENGTH,DIET
Hadrosaurus,1.2,herbivore
Struthiomimus,0.92,omnivore
Velociraptor,1.0,carnivore
Triceratops,0.87,herbivore
Euoplocephalus,1.6,herbivore
Stegosaurus,1.40,herbivore
Tyrannosaurus Rex,2.5,carnivore
Run Code Online (Sandbox Code Playgroud)
NAME,STRIDE_LENGTH,STANCE
Euoplocephalus,1.87,quadrupedal
Stegosaurus,1.90,quadrupedal
Tyrannosaurus Rex,5.76,bipedal
Hadrosaurus,1.4,bipedal
Deinonychus,1.21,bipedal
Struthiomimus,1.34,bipedal
Velociraptor,2.72,bipedal
Run Code Online (Sandbox Code Playgroud)
speed = ((STRIDE_LENGTH / LEG_LENGTH) - 1) * SQRT(LEG_LENGTH * g)
Run Code Online (Sandbox Code Playgroud)
在哪里
g = 9.8 m/s^2
Run Code Online (Sandbox Code Playgroud)
编写一个程序来读取 csv 文件,并仅打印双足恐龙的名称,按速度从最快到最慢排序。
在 SQL 中,这很简单:
select f2.name from
file1 f1 join file2 f2 on f1.name = f2.name
where f1.stance = 'bipedal'
order by (f2.stride_length/f1.leg_length - 1)*pow(f1.leg_length*9.8,0.5) desc
Run Code Online (Sandbox Code Playgroud)
如何在 Bash 或 Python 中完成此操作?
已经创建了一些工具来实现此目的。这是示例:
$ csvq 'select * from cities'
+------------+-------------+----------+
| name | population | country |
+------------+-------------+----------+
| warsaw | 1700000 | poland |
| ciechanowo | 46000 | poland |
| berlin | 3500000 | germany |
+------------+-------------+----------+
$ csvq 'insert into cities values("dallas", 1, "america")'
1 record inserted on "C:\\cities.csv".
Commit: file "C:\\cities.csv" is updated.
Run Code Online (Sandbox Code Playgroud)
https://github.com/mithrandie/csvq