小编Abd*_*res的帖子

如何在Spyder中更改默认模块docstring?

如何在Spyder IDE中更改新模块的默认模板?

# -*- coding: utf-8 -*-
"""
Created on %(date)s

@author: X
"""
Run Code Online (Sandbox Code Playgroud)

module docstring spyder

6
推荐指数
1
解决办法
3489
查看次数

如何将PDF从base64字符串转换为文件?

我有一个PDF作为base64字符串,我需要使用Python将其写入文件。我尝试了这个:

import base64

base64String = "data:application/pdf;base64,JVBERi0xLjQKJeHp69MKMSAwIG9iago8PC9Qcm9kdWNlciAoU2tpYS9..."

with open('temp.pdf', 'wb') as theFile:
  theFile.write(base64.b64decode(base64String))
Run Code Online (Sandbox Code Playgroud)

但是它没有创建有效的PDF文件。我想念什么?

python pdf base64 file-writing

6
推荐指数
3
解决办法
5188
查看次数

如何导入/导出 Spyder 首选项?

如何导入/导出 Spyder 的自定义首选项?例如,如果我将它安装在一台新机器上并且想从我的原始安装中导入我的颜色/解释器/键映射默认值。

spyder

6
推荐指数
1
解决办法
1852
查看次数

如何从Python脚本返回一个值作为Bash变量?

这是我的代码的摘要:

# import whatever

def createFolder():
    #someCode
    var1=Gdrive.createFolder(name)
    return var1 

def main():
    #someCode
    var2=createFolder()
    return var2

if __name__ == "__main__":
    print main()
Run Code Online (Sandbox Code Playgroud)

我设法向bash变量返回值的一种方式是打印从main()返回的内容。另一种方法是只在脚本的任何位置打印变量

有什么办法可以以更Python化的方式返回它吗?

脚本的调用方式如下:

folder=$(python create_folder.py "string_as_arg")
Run Code Online (Sandbox Code Playgroud)

python bash program-entry-point return-value

5
推荐指数
1
解决办法
9916
查看次数

如何使用 Python ijson 读取大型 JSON 文件?

我正在尝试解析一个大的 json 文件(数百个演出)以从其密钥中提取信息。为简单起见,请考虑以下示例:

import random, string

# To create a random key 
def random_string(length):
        return "".join(random.choice(string.lowercase) for i in range(length))

# Create the dicitonary 
dummy = {random_string(10): random.sample(range(1, 1000), 10) for times in range(15)}

# Dump the dictionary into a json file 
with open("dummy.json", "w") as fp:
        json.dump(dummy, fp)
Run Code Online (Sandbox Code Playgroud)

然后,我在 python 2.7 中使用 ijson 来解析文件:

file_name = "dummy.json"

with open(file_name, "r") as fp:

    for key in dummy.keys():

        print "key: ", key 

        parser = ijson.items(fp, str(key) + ".item")

        for number in …
Run Code Online (Sandbox Code Playgroud)

python json python-2.7 ijson

5
推荐指数
1
解决办法
1万
查看次数

JSON文件中的原始字符串字段值

在我的JSON文件中,其中一个字段必须携带另一个文件(字符串)的内容。

该字符串具有CRLF,单/双引号,制表符。

有没有办法将我的整个字符串视为原始字符串,这样我就不必转义特殊字符了?

JSON中的等效字符串与C ++中的字符串原始定界符一样吗?

在C ++中,我只是将整个文件内容放入:R“(...)”

json rawstring

5
推荐指数
2
解决办法
1761
查看次数

Spearman在Python中使用关系排名相关

我想使用Python 计算spearman等级相关性,并且最有可能是scipy实现(scipy.stats.spearmanr).

手头的数据看起来如下,以下方式(字典):

{a:0.3, b:0.2, c:0.2} and {a:0.5, b:0.6, c:0.4}
Run Code Online (Sandbox Code Playgroud)

现在把它传递给spearman模块,如果我是正确的(降序),我会给它们分配等级:

[1,2,3] and [2,1,3]
Run Code Online (Sandbox Code Playgroud)

所以现在我想考虑关系,所以我现在用于第一个向量:

[1,2,2] or [1,2.5,2.5]
Run Code Online (Sandbox Code Playgroud)

基本上,这整个概念是否正确以及如何处理这种基于字典的数据的联系.

正如@Jaime所建议的那样,spearmanr函数可以使用值,但为什么这种行为是可行的:

In [5]: spearmanr([0,1,2,3],[1,3,2,0])
Out[5]: (-0.39999999999999997, 0.59999999999999998)

In [6]: spearmanr([10,7,6,5],[0.9,0.5,0.6,1.0])
Out[6]: (-0.39999999999999997, 0.59999999999999998)
Run Code Online (Sandbox Code Playgroud)

谢谢!

python statistics scipy correlation

4
推荐指数
1
解决办法
1万
查看次数

使用NLTK,如何获得列表的离散图?

使用NLTK导入的数据,我可以调用text1.dispersion_plot(...),但是我无法dispersion_plot()在单词/标记列表中使用该函数.

我开始通过以下方式获得纯文本:

tokens = nltk.word_tokenize(...)
Run Code Online (Sandbox Code Playgroud)

返回列表类型.

text1在NLTK书的背景下是类型class 'nltk.text.Text'.

如何dispersion_plot()在我自己的文本上使用和其他功能?

python nltk

4
推荐指数
1
解决办法
2688
查看次数

自动更新键到整数映射的字典

我有一个类型的功能:

def GetMapping(mappings, key):
    mapping = mappings.get(key)

    if mapping is None:
        currentMax = mappings.get("max", 0)
        mapping = currentMax + 1
        mappings["max"] = mapping
        mappings[key] = mapping

    return mapping, mappings
Run Code Online (Sandbox Code Playgroud)

基本上,给定字典mappings和键key,该函数返回与键相关联的值(如果存在).

如果没有,它会在字典中找到当前最大id,存储在键'max'下,将其分配给此键,并更新max的值.

我想知道是否有一种内置/不那么详细的方法来实现这一目标?

python dictionary

4
推荐指数
1
解决办法
85
查看次数

python中文本清理/处理的管道

我对 python 环境(jupyter notebook)很陌生,我正在尝试处理相对庞大的文本数据。我想通过应用以下步骤并按相同顺序来处理它:

去除空格、小写、词干、去除标点符号但保留词内破折号或连字符、去除停用词、去除符号、去除空格、

我希望我可以得到一个可以执行任务的函数,而不是单独执行它们,是否有任何单个库和/或函数可以提供帮助?如果不是,那么定义一个函数来执行它们的最简单方法是什么?

text-processing nlp nltk python-3.x jupyter-notebook

4
推荐指数
1
解决办法
4424
查看次数