我正在尝试选择表中的行,并使用PETL使用原始表中的信息创建新表。
我现在的代码是:
import petl as etl
table_all = (
etl.fromcsv("practice_locations.csv")
.convert('Practice_Name', 'upper')
.convert('Suburb', str)
.convert('State', str)
.convert('Postcode', int)
.convert('Lat', str)
.convert('Long', str)
)
def selection(post_code):
table_selected = etl.select(table_all, "{Postcode} == 'post_code'")
print(post_code)
etl.tojson(table_selected, 'location.json', sort_keys=True)
但是我似乎无法通过按原样使用选择功能来填充table_selected
。如果我将etl.select
替换为{p>
post_code
输出正确的表格,如下所示:
table_selected = etl.select(table_all, "{Postcode} == 4510")
我确定我只是试图以一种错误的方式调用 +--------------------------------+--------------+-------+----------+--------------+--------------+
| Practice_Name | Suburb | State | Postcode | Lat | Long |
+================================+==============+=======+==========+==============+==============+
| 'CABOOLTURE COMBINED PRACTICE' | 'Caboolture' | 'QLD' | 4510 | '-27.085007' | '152.951707' |
+--------------------------------+--------------+-------+----------+--------------+--------------+
,但是已经尝试了PETL文档中的所有内容,而且似乎无法弄清。
非常感谢您的帮助。
答案 0 :(得分:2)
use strict;
use warnings 'all';
use feature 'say';
use File::Globstar 'globstar';
my @paths = qw{
dou/you/1.txt
wanna/play/2.txt
with/me/3.txt
like/play/4.txt
anything/really/5.txt
};
for my $path ( @paths ) {
say for globstar "a/b/c/d/e/**/$path;
}
不会将"{Postcode} == 'post_code'"
替换为传递给您的post_code
函数的值。
您需要设置选择字符串的格式(使用selection
时转义{Postcode}
)
format
在控制台中测试
table_selected = etl.select(table_all, "{{Postcode}} == {post_code}".format(post_code=post_code))