小编sha*_*nuo的帖子

引号和字段分隔符之间的数据

在下面给出的示例中,未上传最后一行.我收到一个错误:

Data between close double quote (") and field separator: 
Run Code Online (Sandbox Code Playgroud)

这看起来像一个bug,因为管道符号之间的所有数据都应该被视为一个字段.

Schema: one:string,two:string,three:string,four:string

上传文件:

This | is | test only | to check quotes
second | line | "with quotes" | no text
third line | with | "start quote" and | a word after quotes
Run Code Online (Sandbox Code Playgroud)

处理上面的第一行和第二行.但不是第三个.


更新:

有人可以解释为什么以下工作除了第三行

This | is | test only | to check quotes
second | line | "with quotes" | no text
third line | with | "start quote" and | a …
Run Code Online (Sandbox Code Playgroud)

google-bigquery

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

限制多行插入

从版本3.7.11开始,SQLite支持增强的INSERT语法,允许通过VALUES子句插入多行.

http://www.sqlite.org/releaselog/3_7_11.html

是否可以在单个语句中插入多少个值?(例如500)

sqlite

9
推荐指数
2
解决办法
3939
查看次数

使用装饰器来持久化python对象

我从下面链接获得的代码,可以将数据保存到磁盘.

http://tohyongcheng.github.io/python/2016/06/07/persisting-a-cache-in-python-to-disk.html

我试了但是文件没有生成.

import atexit
import pickle
# or import cPickle as pickle

def persist_cache_to_disk(filename):
    def decorator(original_func):
        try:
            cache = pickle.load(open(filename, 'r'))
        except (IOError, ValueError):
            cache = {}

        atexit.register(lambda: pickle.dump(cache, open(filename, "w")))

        def new_func(*args):
            if tuple(args) not in cache:
                cache[tuple(args)] = original_func(*args)
            return cache[args]

        return new_func

    return decorator
Run Code Online (Sandbox Code Playgroud)

我尝试按照示例使用此代码...

@persist_cache_to_disk('users.p')
def get_all_users():
    x = 'some user'
    return x
Run Code Online (Sandbox Code Playgroud)

更新:

这是在python命令提示符下工作,但在ipython笔记本中不起作用.

python ipython-notebook jupyter-notebook

9
推荐指数
2
解决办法
1197
查看次数

为文件中的每个单词创建一个字典,并计算其后的单词的频率

我正在努力解决一个棘手的问题并迷失方向.

这是我应该做的:

INPUT: file
OUTPUT: dictionary

Return a dictionary whose keys are all the words in the file (broken by
whitespace). The value for each word is a dictionary containing each word
that can follow the key and a count for the number of times it follows it.

You should lowercase everything.
Use strip and string.punctuation to strip the punctuation from the words.

Example:
>>> #example.txt is a file containing: "The cat chased the dog."
>>> with open('../data/example.txt') as f: …
Run Code Online (Sandbox Code Playgroud)

python counter dictionary nltk n-gram

9
推荐指数
2
解决办法
3136
查看次数

逃避单引号

我有一张这样的桌子......

select * from myescape;
+-----------+
| name      |
+-----------+
| shantanu' |
| kumar's   |
+-----------+
2 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

我需要用'\'替换单引号

我还需要避免双引号和反斜杠.

mysql

8
推荐指数
2
解决办法
2万
查看次数

删除多行注释

如果它们以/*开头并以*结尾,我如何删除所有注释?我尝试了以下操作.它适用于一行评论.

sed '/\/\*/d' 
Run Code Online (Sandbox Code Playgroud)

但它不会删除多行注释.例如,不删除第二行和第三行.

/*!50500 PARTITION BY RANGE (TO_SECONDS(date_time ))
 PARTITION 20120102parti VALUES LESS THAN (63492681600),
(PARTITION 20120101parti VALUES LESS THAN (63492595200) */ ;
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我需要保留最后一个; 在结束评论标志后.

awk sed

8
推荐指数
3
解决办法
2万
查看次数

使用heredoc创建新的文本文件

在我的shell脚本中,我使用heredoc块来动态创建文件.什么是python等价物?

cat > myserver.pem << "heredoc"
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAnTsiYssvsuM1DRjyhqD8+ZB8ESqUFHgzeBYONp3yqjK8ICw/LRrxjXGXidAW
aPBXfktv3zN/kFsLMEFJKrJs/TLCfXG1CwFHMZzJRLM4aE6E0j6j+KF96cY5rfAo82rvP5kQdTIm
-----END RSA PRIVATE KEY-----
heredoc
Run Code Online (Sandbox Code Playgroud)

我正在寻找一个简单的解决方案.我非常喜欢上面的shell脚本代码.我可以在python中"按原样"使用它吗?

python heredoc

8
推荐指数
1
解决办法
2736
查看次数

动态地向类提供变量

我试过这里的官方教程提供的例子:

http://saratoga.readthedocs.org/en/latest/serviceclasses.html

我对代码做了一些更改,这就是它的样子:

http://23.21.167.60:8094/v1/yearlength?name=earth
Run Code Online (Sandbox Code Playgroud)

我的问题是我需要在URL中提供account = 211829,就像name = earth一样.

我在下面写的是工作,因为我已经为班级提供了帐号.我该如何动态执行此操作?

import json
from saratoga.api import SaratogaAPI, DefaultServiceClass

class PlanetServiceClass(DefaultServiceClass):
    def __init__(self, myaccount):
        self.yearLength = {
            "earth": self.myquery(myaccount),
            "pluto": {"seconds": 7816176000}
        }

    def myquery(self, myaccount):
        import pandas as pd

        query = ('select * from mydata198 where account = %s  ')  % (myaccount)

        import sqlalchemy
        engine = sqlalchemy.create_engine('mysql+pymysql://dba:pass@1.2.3.4/test')
        conn = engine.raw_connection()

        df=pd.read_sql(query, conn)
        return df.to_json()

class PlanetAPI(object):
    class v1(object):
        def yearlength_GET(self, request, params):
            planetName = params["params"]["name"].lower()
            return self.yearLength.get(planetName) …
Run Code Online (Sandbox Code Playgroud)

python saratoga

8
推荐指数
1
解决办法
192
查看次数

将函数转换为Lambda

我有这个功能在python中按预期工作.如何将其转换为AWS Lambda函数?

def mymailgun(url):    
    import urllib2
    myfile=urllib2.urlopen(url)

    import requests
    print requests.post("https://api.mailgun.net/v3/XXX.mailgun.org/messages",
                        auth=("api", "key-XXX"),
                        files=[("attachment", myfile)
                               #("attachment", open("files/test.txt"))
                               ],
                        data={"from": "Excited User <excited-user@example.com>",
                              "to": "XXX@gmail.com",
                              "cc": "YYY@yahoo.com",
                              "bcc": "ZZZ@hotmail.com",
                              "subject": "Hello",
                              "text": "Testing some awesomness with attachments!",
                              "html": myfile})
Run Code Online (Sandbox Code Playgroud)

python aws-lambda

8
推荐指数
1
解决办法
2862
查看次数

将 CloudFormation 模板 (YAML) 转换为对流层代码

我有一个用 Yaml 编写的大型 CloudFormation 模板,我想开始使用对流层。有没有什么简单的方法可以将 CF 模板转换为对流层代码?

我在这里注意到这个脚本https://github.com/cloudtools/troposphere/blob/master/troposphere/template_generator.py 这会创建一个对流层 python 对象,但我不确定是否可以输出它对流层代码。

aws-cloudformation troposphere aws-cdk

8
推荐指数
1
解决办法
1223
查看次数