有很多关于 json to pandas 数据框的问题,但没有一个能解决我的问题。我正在练习这个看起来像这样的复杂 json 文件
{
"type" : "FeatureCollection",
"features" : [ {
"Id" : 265068000,
"type" : "Feature",
"geometry" : {
"type" : "Point",
"coordinates" : [ 22.170376666666666, 65.57273333333333 ]
},
"properties" : {
"timestampExternal" : 1529151039629
}
}, {
"Id" : 265745760,
"type" : "Feature",
"geometry" : {
"type" : "Point",
"coordinates" : [ 20.329506666666667, 63.675425000000004 ]
},
"properties" : {
"timestampExternal" : 1529151278287
}
} ]
}
Run Code Online (Sandbox Code Playgroud)
我想使用pd.read_json()我的主要目标将这个 json 直接转换为 Pandas 数据帧,我的主要目标是提取 Id、坐标和时间戳外部。由于这是非常复杂的 …
这是我的代码:
@click.group()
@click.pass_context
@click.argument('CHALLENGE', type=int)
def challenge(ctx, challenge):
ctx.obj = Challenge(challenge=challenge)
@click.group(invoke_without_command=True, cls=PhaseGroup)
@click.pass_obj
@click.argument('PHASE', type=int)
def phase(ctx, phase):
# Something
challenge.add_command(phase)
Run Code Online (Sandbox Code Playgroud)
这些命令一起工作应该是这样的。
cli challenge 1 phase 1
Run Code Online (Sandbox Code Playgroud)
通过正确执行,它可以按预期工作。
但是当我使用 --help 或在阶段上定义任何其他标志时,它会抛出
cli challenge 1 phase 1 --help
It throws Error: Missing argument "PHASE".
Run Code Online (Sandbox Code Playgroud)
我搜索了 SOF,发现python click app 失败,并显示“缺少参数”,但无法找到问题这是一个有同样问题的用户,但我不太明白答案。
我不能将参数设置为可选,因为它是 CLI 工作的关键部分。
寻求专家的帮助,以帮助我在创建的程序中做出正确的选择.创建列表的两种方法中哪一种看起来更像Pythonic并且对您可读?或者,有没有更好的方法可以做到这一点?
def test_func(*args):
s = 'Country name: United {nm}'
l = [s.format(nm='States') if x is 'us'
else s.format(nm='Arab Emirates') if x is 'uae'
else s.format(nm='Kingdom') if x is 'uk'
else 'Unknown' for x in args]
return l
# execute
test_func('us', 'uk', 'uae')
# results
['Country name: United States',
'Country name: United Kingdom',
'Country name: United Arab Emirates']
Run Code Online (Sandbox Code Playgroud)
def test_func(*args):
s = 'Country name: United {nm}'
l = []
for arg in args:
if arg …Run Code Online (Sandbox Code Playgroud) 我一直使用pandas和numpy处理一些数据,直到得到两个类似的数组输出:
array(['french', 'mexican', 'cajun_creole', ..., 'southern_us', 'italian',
'thai'], dtype='<U12')
array(['french', 'mexican', 'cajun_creole', ..., 'jamaican', 'italian',
'thai'], dtype=object)
Run Code Online (Sandbox Code Playgroud)
我看不出有什么区别<U12?
for我在 Pycharm IDE 中的 Python 中有一个循环。我有 20 次循环迭代for。然而,该错误似乎来自第 18 次迭代期间循环的数据集。是否可以跳过 for 循环的前 17 个值,而仅跳转到调试第 18 次迭代?
目前,我已经经历了所有 17 次迭代,直到第 18 次迭代。循环中包含的逻辑for相当复杂且冗长。因此,每次迭代的每个调试周期都需要很长时间。
有没有某种方法可以跳到 Pycharm 中所需的迭代,而不需要对先前的迭代进行深入调试?
我正在努力为我的课程学习更多的分数,我的老师说这会有所帮助.在创建变量或表等时,我想知道如何对其进行编码,以便如果它已经存在则不会运行代码.
$sql2 = "CREATE TABLE referee(refereeID INT (5) NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20))";
if (mysqli_query($link, $sql)){
echo "Table created successfully";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用更新或创建模型,如下所示:
device_inv = Inventory.objects.update_or_create(
defaults={
'location' : site,
'device' : dev_name
},
device = dev_name
)
Run Code Online (Sandbox Code Playgroud)
我认为我可以使用 device_inv.id 的帖子。但是 printi device_inv 我得到一个对象和 False,我认为它是对象以及它是更新还是创建的答案?(真被创建,假被编辑)
>>> print device_inv
(<Inventory: Inventory object>, False)
Run Code Online (Sandbox Code Playgroud)
我还尝试了几种尝试访问该对象的方法,但似乎都没有奏效。
>>> for i in device_inv[0]:
... print i
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'Inventory' object is not iterable
>>> print device_inv[0]["id"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'Inventory' object has no attribute '__getitem__'
Run Code Online (Sandbox Code Playgroud)
有人能指出我正确的方向吗?
我在php 7.1上运行简单的html dom.
但是第一行我无法解析html
<?php
include 'simple_html_dom.php';
$html = file_get_html('http://google.com');
echo $html;
?>
Run Code Online (Sandbox Code Playgroud)
该页面不显示任何内容(白色背景)和上述代码.
但是下面的代码却运行:
<?php
include 'simple_html_dom.php';
//base url
$base = 'https://google.com';
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $base);
curl_setopt($curl, CURLOPT_REFERER, $base);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$str = curl_exec($curl);
curl_close($curl);
// Create a DOM object
$html_base = new simple_html_dom();
// Load HTML from a string
$html_base->load($str);
echo $html_base;
$html_base->clear();
unset($html_base);
?>
Run Code Online (Sandbox Code Playgroud)
然后,我尝试使用以上代码获取img类代码,但没有工作:
获取图片html:
<div class="product_thumb">
<a title="Me Before You" class="image-border" href=/me-before-you-a-novel-movie-tie-in-p69988.html"> …Run Code Online (Sandbox Code Playgroud) list1=[1,3,8,10,23,8,8,10,23,3,8,10,23,3,8,10,23]
list2=[10,23,3]
cnt=list1.count(list2[0])
cnt1=1
j=0
while (cnt1<=cnt):
list3=[]
list3.append(list2[0])
i=1
k=list1.index(list2[0])
while (i<len(list2)):
list3.append(list1[k+i])
i=i+1
print (list3)
if (list2==list3):
print ("list2 is a subset")
j=j+1
else:
print ("list2 is not a subset")
list1.remove(list2[0])
cnt1=cnt1+1
print (list2,"occurs",j,"times")
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误.
Run Code Online (Sandbox Code Playgroud)Traceback (most recent call last): File "C:\Python26\Lib\idlelib\sublist.py", line 12, in <module> list3.append(list1[k+i]) IndexError: list index out of range"
我有一个包含多个数字的字符串,需要在某些(不是全部)数字中添加前导零.只有单个数字并且前面有字母的数字需要前导零.
输入: "Z9_M50_P3_2X_MY_STRING"
输出: "Z09_M50_P03_2X_MY_STRING"