我正在发送请求,但即使请求成功(我正在与之交互的API,成功发送OTP),我也会收到异常.
例外是:
com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 2 path $
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1573)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1423)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:575)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:429)
at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:349)
at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:346)
at com.google.gson.TypeAdapter.fromJson(TypeAdapter.java:256)
at retrofit.GsonConverter.fromBody(GsonConverter.java:42)
at retrofit.OkHttpCall.parseResponse(OkHttpCall.java:144)
at retrofit.OkHttpCall.access$000(OkHttpCall.java:25)
at retrofit.OkHttpCall$1.onResponse(OkHttpCall.java:90)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Run Code Online (Sandbox Code Playgroud)
现在,我怎么会看到格式错误的JSON?为了知道是什么Json对象格式错误,如果它是被返回的对象(我期望它是一个字符串),或者它是我发送的对象.
请原谅我,如果这是一个微不足道的问题,我本周才开始使用Android开发.
这是服务:
public static EnrollmentApiInterface getApiClient(){
if (EnrollmentRequest == null) {
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request()); …Run Code Online (Sandbox Code Playgroud) 我看过这个问题/答案,还有更多喜欢它,但我仍然无法通过切换选项卡或移动到带有错误的选项卡进行表单验证.
相关的jQuery:
$(document).ready(function () {
$("form[name=modify]").validate({
ignore: [],
rules: {
first_name: {
required: true,
},
surname: {
required: true
},
id_number: {
required: true
},
mobile_number: {
number: true,
minlength: 10,
},
home_number: {
number: true,
minlength: 10,
},
other_contact: {
number: true,
minlength: 10,
},
line1: {
required: true,
},
city_town: {
required: true,
},
postal_code: {
required: true,
minlength: 4,
},
curr_renumeration: {
required: true,
number: true,
},
expect_renumeration: {
required: true,
number: true
},
email: …Run Code Online (Sandbox Code Playgroud) 我有一套相当复杂的sqlalchemy模型(所有模型都使用DeclarativeBase声明,通过flask-sqlalchemy的db.Model类).业务逻辑如下:
我准备了一个示例应用程序来演示核心功能:
#!/usr/bin/env python
from __future__ import print_function
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
import sqlalchemy_defaults
from flask.ext.script import Manager, Server
app = Flask(__name__)
app.config.update(
DEBUG=True,
SQLALCHEMY_DATABASE_URI=(
'postgresql://sqlareltest:sqlareltest' +
'@localhost:5432/sqlareltest'),
)
db = SQLAlchemy(app)
db.Column = sqlalchemy_defaults.Column
sqlalchemy_defaults.make_lazy_configured(db.mapper)
class Task(db.Model):
id = db.Column(db.Integer(), primary_key=True)
title = db.Column(db.String(255), default='')
def __repr__(self):
return self.title
class Submission(db.Model):
id = db.Column(db.Integer(), primary_key=True)
title = db.Column(db.String(255), default='')
def __repr__(self):
return self.title
class TaskQuestion(db.Model):
id = db.Column(db.Integer(), primary_key=True)
task_id = db.Column(db.Integer, …Run Code Online (Sandbox Code Playgroud) 我有以下字符串输入:24052017.当我尝试做的时候:
>>>dateutil.parser.parse("24052017")
Run Code Online (Sandbox Code Playgroud)
它告诉我month must be in 1..12.
我甚至尝试过:
>>>dateutil.parser.parse("24052017", firstday=True)
Run Code Online (Sandbox Code Playgroud)
它给了我完全相同的结果.
似乎发生的事情是,它不喜欢没有空格或分隔符的事实.它正确地读取了一天,但是当它读到的那个月0520.这是我怀疑的,至少.
如何在dateutil.parser不操纵字符串的情况下使用转换此特定输入?
我是Python的新手.我'if statement'在我的jinja2模板中有这个:
{{context.disabled==True and "Yes" or "No"}}
Run Code Online (Sandbox Code Playgroud)
我不确定这个语法叫什么,但它的作用就像一个if.
PHP等价物将是: echo $disabled ? "Yes" : "No"
这总是回来No,False即使context.disabled是True,
它为什么会这样?我不知道为什么会这样做,我做错了吗?
我有一个类似于以下内容的列表:
industries_list = ["Computers, Internet","Photography, Tourism","Motoring, Manufacturing"]
Run Code Online (Sandbox Code Playgroud)
如何拆分此列表,以便输出类似于:
industries_list = [["Computers","Internet"],["Photography","Tourism"],["Motoring","Manufacturing"]]
Run Code Online (Sandbox Code Playgroud)
我尝试将其转换为字符串,用逗号分割,然后将其放回列表中,但这并没有给出我想要的结果。
如何将数据添加到 mongoengine 的字典中?
我没有再次保存整个字典的数据,我一次只想添加一个项目。
我试过使用:
Lookups.objects(pk="52d3a8e318fbaf0e1075de4f").update(push__schools=new_schools)
Run Code Online (Sandbox Code Playgroud)
但我认为这不起作用的原因push是将项目添加到列表,而不是字典。谁能帮帮我吗?
在将此标记为重复之前:
我确实看了这个问题/答案,我确实做了它的建议,但当我添加这个代码时:
permslookup = sa.Table('permslookup',
sa.Column('perms_lookup_id', primary_key=True),
sa.Column('name', sa.Unicode(40), index=True),
sa.Column('description', sa.Text),
sa.Column('value', sa.Numeric(10, 2)),
sa.Column('ttype', sa.PickleType(), index=True),
sa.Column('permission', sa.Unicode(40), index=True),
sa.Column('options', sa.PickleType())
)
Run Code Online (Sandbox Code Playgroud)
然后运行alembic upgrade head,我收到以下错误:
AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema'
Run Code Online (Sandbox Code Playgroud)
当我检查完整的堆栈跟踪时,我注意到这导致了错误:
sa.Column('options', sa.PickleType())
Run Code Online (Sandbox Code Playgroud)
这是上面代码的最后一行......我该如何解决这个问题?我不知道如何解决它...任何形式的帮助将不胜感激.
这是我要插入的数据:
op.bulk_insert('permslookup',
[
{
'id': 1,
'name': 'accounts',
'description': """ Have permission to do all transactions """,
'value': 1,
'ttype': ['cash', 'loan', 'mgmt', 'deposit', 'adjustments'],
'permission': 'accounts',
'options': None
},
{
'id': 2,
'name': …Run Code Online (Sandbox Code Playgroud) 请考虑以下内容List of Dictionaries(从数据库中检索):
education = [{
"schools" : "Nelson Mandela Metropolitan University",
"studied_from" : new Date("16/1/2012 02:00:00"),
"studied_to" : new Date("25/1/2055 02:00:00"),
"qualifications" : " National Diploma",
"fields_of_study" : "Industrial engineering"
}, {
"schools" : "Massachusetts Institute of Technology",
"studied_from" : new Date("16/1/2009 02:00:00"),
"studied_to" : new Date("25/1/2020 02:00:00"),
"qualifications" : "B Tech",
"fields_of_study" : "Information Technology"
}]
Run Code Online (Sandbox Code Playgroud)
我试图从一个dict返回数据.
我尝试过以下方法:
for edu in context.education:
studied_to.append(edu.studied_to)
qualification = edu.qualifications
fields_of_study = edu.fields_of_study
school = edu.schools
recent = …Run Code Online (Sandbox Code Playgroud) 我有CSV文件,其中包括日期,看起来像:
2014-01-25 00:14:08.000
Run Code Online (Sandbox Code Playgroud)
现在我解析CSV文件并获取日期.然后我运行一个正则表达式来检查日期是否有效(匹配上面的格式)然后我将日期从CSV文件转换为DateTime.
这怎么能给我一个ValueError:陈述:need more than 3 values to unpack.CSV文件可以包含数千条记录,其日期与上述类似.
我的正则表达式:
date_re_time_norm = re.compile("""^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[ ](\d\d:\d\d:\d\d.\d\d\d)$""", re.I)
Run Code Online (Sandbox Code Playgroud)
我的转换方法:
def convert_date(self, dstring):
if self.date_re_time_norm.match(dstring):
year, month, day, hour, minute, second = str(dstring).split('-')
return datetime(int(year), int(month), int(day), int(hour), int(minute), int(second), 0, 0, 0, 0, 0, 0)
Run Code Online (Sandbox Code Playgroud)
方法缩短了可读性.
难道我做错了什么?
我有以下内容:
@periodic_task(run_every=crontab(minute="*/1"))
def PeriodicUpdateAgentLastRica():
query = """update agents
inner join
(select sims.consignment_agents_id as agents_id,
MAX(sims.rica_current_stamp)
as last_rica_stamp
from sims
where sims.rica_current_stamp > DATE_SUB(now(), INTERVAL 3 MONTH) and sims.consignment_agents_id is not NULL
group by sims.consignment_agents_id) as dt on dt.agents_id = agents.id
set agents.last_rica_stamp = dt.last_rica_stamp"""
res = Session.execute(query)
print res
if res.rowcount:
log.info("Updated %s agents" % res.rowcount)
#transaction.commit()
Run Code Online (Sandbox Code Playgroud)
当我运行时celery worker -B这是返回的内容:
[2014-02-12 09:15:00,012: INFO/MainProcess] Received task: prepaid.models.Prepaid.PeriodicUpdateAgentLastRica[9ca091c8-595f-4163-8ddf-2742e573b90c]
[2014-02-12 09:15:01,812: WARNING/Worker-7] <sqlalchemy.engine.result.ResultProxy object at 0x776f310>
[2014-02-12 09:15:01,813: INFO/Worker-7] Updated 2923 …Run Code Online (Sandbox Code Playgroud) 我有一个漫长而复杂的方法,我将给出重要的部分:
def get_itter(self, sort_by=0): #sort_by is set to 0 for debugging purposes
...
r = self.parser(self.in_file) # parse a csv file
...
if type(sort_by) == int:
r = [i for i in r]
sort = sorted(r, key=sort_by)
...
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,当我运行此代码时,它给了我一个错误:TypeError: 'int' object is not callable. 为什么给我这个错误?
PS 我对 Python 比较陌生,正在尝试向我没有编写的代码添加一些功能。
我知道要填充字符串,你会做类似的事情:
>>> n = '6'
>>> print n.zfill(3)
>>> '006
Run Code Online (Sandbox Code Playgroud)
和一个数字:
>>> n = 6
>>> print '%03d' % n
>>> 006
>>> print "{0:03d}".format(6) # python >= 2.6
>>> 006
>>> print("{0:03d}".format(6)) # python 3
>>> 006
Run Code Online (Sandbox Code Playgroud)
我的问题是:有没有办法撤消/取消/取消填充字符串和数字?
python ×10
sqlalchemy ×3
dictionary ×2
list ×2
mysql ×2
alembic ×1
android ×1
csv ×1
datetime ×1
for-loop ×1
if-statement ×1
javascript ×1
jinja2 ×1
jquery ×1
json ×1
mongodb ×1
mongoengine ×1
padding ×1
pyramid ×1
regex ×1
relationship ×1
retrofit ×1
sorting ×1
tabs ×1
transactions ×1