谁能告诉我如何将参数传递给装饰器调用函数?
def doubleIt(Onefunc):
def doubleIn():
return Onefunc()*Onefunc()
return doubleIn
@doubleIt
def Onefunc():
return 5
print(Onefunc()) # it prints out 25.
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试升级Onefunc()到:
@doubleIt
def Onefunc(x):
return x
Run Code Online (Sandbox Code Playgroud)
我面临以下错误:
TypeError
Traceback (most recent call last)
<ipython-input-17-6e2b55c94c06> in <module>()
9
10
---> 11 print(Onefunc(5))
12
TypeError: doubleIn() takes 0 positional arguments but 1 was given
Run Code Online (Sandbox Code Playgroud)
错误是自我解释,但我不知道如何更新doubleIn()函数来处理它.
如果我有一个字符串,它包含很多单词.如果字符串中的单词不是以字母开头的话,我想删除右括号_.
示例输入:
this is an example to _remove) brackets under certain) conditions.
Run Code Online (Sandbox Code Playgroud)
输出:
this is an example to _remove) brackets under certain conditions.
Run Code Online (Sandbox Code Playgroud)
如果不拆分使用单词,我怎么能这样做re.sub呢?
我有一个目录,其中可以包含其他文件夹中的许多文件夹,以及其中的 txt 文件。我想返回至少包含一个 .txt 文件的所有目录的列表。
我正在尝试以下递归方法,但它不起作用:
def getDirectoryList(dir):
directoryList = []
# return nothing if dir is a file
if not os.path.isdir(dir):
return []
# add dir to directorylist if it contains .txt files
if len([file for file in os.listdir(dir) if file.endswith('.txt')])>0:
directoryList.append(dir)
for d in os.listdir(dir):
for x in getDirectoryList(d):
directoryList.append[x]
return directoryList
Run Code Online (Sandbox Code Playgroud) 我正在遍历文件夹并收集要加载到数据库中的文档名称和其他一些数据。
import os
text_file = open("Output.txt", "w")
dirName = 'D:\\'
for nextDir, subDir, fileList in os.walk(dirName):
for fname in fileList:
text_file.write(fname + '\n')
Run Code Online (Sandbox Code Playgroud)
问题是某些文档名称包含外来字符,例如:
RC-0964_1000 T??ng th??ng Diamond tr? nh?t Vi?t Nam - ??ng Vi?t Th?ng và Tr?n Thu Ph??ng
Run Code Online (Sandbox Code Playgroud)
和
RC-1046 ??2013ARTISTRY??????-?????????_????????Suit & Tie?.mp4
Run Code Online (Sandbox Code Playgroud)
上面的代码在最后一行给了我这个错误:
UnicodeEncodeError: 'charmap' codec can't encode characters at positions ##-##:character maps to (undefined)
Run Code Online (Sandbox Code Playgroud)
我试过
temp = fname.endcode(utf-8)temp = fname.decode(utf-8)temp = fname.encode('ascii','ignore')
temp2 = temp.decode('ascii')temp =unicode(fname).encode('utf8')如何编写此脚本以将所有字符写入文件?我是否需要更改我正在写入的文件或正在写入的字符串,以及如何更改?
这些名字都可以成功粘贴到文件中,那么Python为什么不写进去呢?
我有一个正整数变量,其值可以在0到999之间.然后将该整数传递给软件.
要传入此软件,整数应始终为3位数.但问题是,它应该有尾随零.
例如:
1 should be passed as 100
19 should be passed as 190
255 should be passed as 255
Run Code Online (Sandbox Code Playgroud)
可以通过检查变量的长度并乘以10或100来完成,具体取决于长度.但有没有更好的Python替代方案.这对于值0也不起作用.
(注意:尾随零是因为整数值实际上是一个毫秒值.软件逐位读取值,它总是需要3位数)
我在Amazon AWS EC-2(Amazon Linux AMI 2015.09.1(HVM),SSD卷类型 - ami-60b6c60a)上安装了一个闪亮的服务器.服务器适用于Shiny应用程序,但不会渲染.rmd,甚至是示例页面中的rmd.
我只看到这个错误:
错误:运行命令错误
在HTML中:
<div id="__reactivedoc__" class="shiny-html-output shiny-bound-output shiny-output-error">error in running command</div>
Run Code Online (Sandbox Code Playgroud)
我已经安装了rmarkdown软件包,它在Rstudio上工作正常.(我为Rstudio和Shiny服务器使用相同的用户).
我不知道如何获得有关错误的更多信息,我没有看到其他消息.
有人有想法吗?
新信息:我调查来自服务器的日志:这是/ var/log/shiny-server /的输出:
su: ignore --preserve-environment, it's mutually exclusive to --login.
Listening on http://127.0.0.1:52294
/opt/shiny-server/ext/pandoc/pandoc: error while loading shared libraries: libgmp.so.3: cannot open shared object file: No such file or directory
Warning: Error in system: error in running command
Stack trace (innermost first):
97: system
96: force
95: with_pandoc_safe_environment
94: get_pandoc_version
93: FUN
92: lapply
91: …Run Code Online (Sandbox Code Playgroud) Azure Rate Card API返回MeterRates字段(请参阅文档).Azure UsageAggregate提供数量(请参阅文档).
根据azure支持页面.这是提问的论坛.
那么,如何应用电表费率?
示例电表费率:
{"0":20, "100":15, "200":10}
Run Code Online (Sandbox Code Playgroud)
如果我有175量是量100*20 + 75*15还是175*15?
为什么要指定包含数量?
示例:rates:{"0":23}使用包含的quantitiy 10可以表示为费率:
{"0":0, "10":23}
Run Code Online (Sandbox Code Playgroud) 我有一个这样的列表:
List1: [1, 5, 9, 1, 5, 9, 15, 21, 29, 1, 5, 9, 15]
Run Code Online (Sandbox Code Playgroud)
我想要一个新的列表,它应该包含最高的数字,然后再用1开始.
List_new: [9, 29, 15]
Run Code Online (Sandbox Code Playgroud)
我试过这个:
List_new = []
for i in range(len(List1)):
j = List1[i]
if j + 1 == '1':
List_new += [j]
else:
continue
print(j)
Run Code Online (Sandbox Code Playgroud)
但我得到了一个空列表.
我将数据存储在计数器中,例如
industries = Counter({'Automotive': 17, 'Commercial Banks': 10, 'Insurance': 4, 'Hospitals': 2, 'Other': 2})
Run Code Online (Sandbox Code Playgroud)
我有兴趣查找特定变量的'index'(即'rank').例如,如果我有兴趣Automotive返回它具有最高密钥,则commercial banks具有第二高.
目前我正在转换为列表,然后查找该项目的索引.例如
industries_list = [key[0] for key in industries.most_common()]
rank_of_automotive = industries_list.index("Automotive")
Run Code Online (Sandbox Code Playgroud)
有没有办法直接从计数器形成这个,而无需转换为列表?
[显然,我知道我当前的代码不处理正确的值相同的变量-即虽然Hospitals和Other这两者都具有值2,我可能会返回一个不同的等级为他们.这对我来说不是一个大问题,但如果两者都返回相同的值,也会发现]
在烧瓶中制作一个网页,用户可以在该网页上选择多个选项。根据这些选项,我想在下一页显示它们的选择选项。我的HTML代码:
<input type="checkbox" name="name1">name1<br>
<input type="checkbox" name="name2">name2<br>
<input type="checkbox" name="name3">name3<br>
Run Code Online (Sandbox Code Playgroud)
我的Python代码:
if checkbox 1 is checked:
do something
if checkbox 2 is checked:
do something different
if checkbox 3 is checked:
do another something different
Run Code Online (Sandbox Code Playgroud)
我找不到如何形成这些if语句的方法。我发现了一些东西:
.getvalue
Run Code Online (Sandbox Code Playgroud)
但是后来我得到了一个错误。使用单选按钮,我可以使其工作。当我给它们起同样的名字并给它们不同的值时:
option = request.form['name']
Run Code Online (Sandbox Code Playgroud)
但是,这也不适用于复选框,因为它只记住最后一个复选框,但我要全部使用。
python ×8
python-3.x ×2
azure ×1
checkbox ×1
directory ×1
encoding ×1
flask ×1
html ×1
list ×1
python-2.7 ×1
r ×1
r-markdown ×1
regex ×1
shiny ×1
shiny-server ×1
unicode ×1
utf-8 ×1