相关疑难解决方法(0)

Pythonic方式创建一个长多行字符串

我有一个很长的查询.我想在Python中将它分成几行.在JavaScript中实现它的一种方法是使用几个句子并将它们与+运算符连接(我知道,也许这不是最有效的方法,但我并不关心这个阶段的性能,只是代码可读性) .例:

var long_string = 'some text not important. just garbage to' +
                  'illustrate my example';
Run Code Online (Sandbox Code Playgroud)

我尝试在Python中做类似的事情,但它不起作用,所以我习惯\拆分长字符串.但是,我不确定这是否是唯一/最好/最好的方式.看起来很尴尬.实际代码:

query = 'SELECT action.descr as "action", '\
    'role.id as role_id,'\
    'role.descr as role'\
    'FROM '\
    'public.role_action_def,'\
    'public.role,'\
    'public.record_def, '\
    'public.action'\
    'WHERE role.id = role_action_def.role_id AND'\
    'record_def.id = role_action_def.def_id AND'\
    'action.id = role_action_def.action_id AND'\
    'role_action_def.account_id = ' + account_id + ' AND'\
    'record_def.account_id=' + account_id + ' AND'\
    'def_id=' + def_id
Run Code Online (Sandbox Code Playgroud)

python string multiline multilinestring

1160
推荐指数
21
解决办法
102万
查看次数

如何在Python中进行换行(换行)?

我有一长串代码,我希望在多行之间分解.我使用什么,语法是什么?

例如,添加一串字符串,

e = 'a' + 'b' + 'c' + 'd'
Run Code Online (Sandbox Code Playgroud)

并将它分成两行:

e = 'a' + 'b' +
    'c' + 'd'
Run Code Online (Sandbox Code Playgroud)

python syntax line-breaks long-lines

979
推荐指数
9
解决办法
144万
查看次数

什么是PEP8的E128:延续线缩进为视觉缩进?

刚刚用Sublime Text(Sublime Linter)打开一个文件,发现了一个我以前从未见过的PEP8格式错误.这是文字:

urlpatterns = patterns('',
    url(r'^$', listing, name='investment-listing'),
)
Run Code Online (Sandbox Code Playgroud)

它正在标记第二个参数,即开始的行 url(...)

我准备在ST2中禁用这个检查,但是我想知道在我忽略它之前我做错了什么.你永远不知道,如果看起来重要我甚至可能改变我的方式:)

python pep8 sublimetext2

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

为什么PEP-8指定的最大行长度为79个字符?

为什么在这个千年中Python PEP-8应该指定最大行长度为79个字符?

几乎每个阳光下的代码编辑器都可以处理更长的行.如何处理包装应该是内容使用者的选择,而不是内容创建者的责任.

在这个时代,有没有(合法的)充分理由坚持79个角色?

python pep8

219
推荐指数
7
解决办法
10万
查看次数

用Python包裹长行

如何在不牺牲缩进的情况下在Python中包装长行?

例如:

def fun():
    print '{0} Here is a really long sentence with {1}'.format(3, 5)
Run Code Online (Sandbox Code Playgroud)

假设这超过了79个字符的建议限制.我读它的方式,这里是如何缩进它:

def fun():
    print '{0} Here is a really long \
sentence with {1}'.format(3, 5)
Run Code Online (Sandbox Code Playgroud)

但是,使用这种方法,连续行的缩进与缩进相匹配fun().这看起来有点难看.如果有人要查看我的代码,那么由于这个print陈述而导致缩进不均匀会很糟糕.

如何在不牺牲代码可读性的情况下有效地缩进这样的行?

python string

192
推荐指数
6
解决办法
22万
查看次数

Python样式 - 用字符串继续行?

在尝试遵守python样式规则时,我将编辑器设置为最多79列.

在PEP中,它建议在括号,括号和括号内使用python隐含的延续.但是,当我达到col限制时处理字符串时,它会有点奇怪.

例如,尝试使用多行

mystr = """Why, hello there
wonderful stackoverflow people!"""
Run Code Online (Sandbox Code Playgroud)

将返回

"Why, hello there\nwonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)

这有效:

mystr = "Why, hello there \
wonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)

因为它返回:

"Why, hello there wonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)

但是,当语句缩进几个块时,这看起来很奇怪:

do stuff:
    and more stuff:
        and even some more stuff:
            mystr = "Why, hello there \
wonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)

如果您尝试缩进第二行:

do stuff:
    and more stuff:
        and even some more stuff:
            mystr = "Why, hello there \
            wonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)

你的字符串最终为:

"Why, hello there                wonderful stackoverflow …
Run Code Online (Sandbox Code Playgroud)

python coding-style

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

Python中的多行f字符串

我正在尝试为国内项目编写符合PEP-8的代码(我必须承认这些是我在python世界中的第一步)并且我有一个超过80个字符长的f字符串

- self.text上点附近的实线是80个字符.(抱歉没有预览的悲伤链接,但我必须有10+代表发布'em)

我试图把它分离成在最不一样的行Python的方式,但实际工作的唯一aswer是我棉短绒错误

工作守则:

def __str__(self):
    return f'{self.date} - {self.time},\nTags:' + \
    f' {self.tags},\nText: {self.text}'
Run Code Online (Sandbox Code Playgroud)

输出:

2017-08-30 - 17:58:08.307055,
Tags: test tag,
Text: test text
Run Code Online (Sandbox Code Playgroud)

linter认为我不尊重PEP-8中的E122,有没有办法让字符串正确并符合代码?

python string python-3.6

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

如何打破PEP8合规性的长串线?

我在项目中有很多这样的长线,并且不知道如何打破它以保持PEP8的快乐.PEP8显示警告.format(me['id'])

pic_url = "http://graph.facebook.com/{0}/picture?width=100&height=100".format(me['id'])
Run Code Online (Sandbox Code Playgroud)

我怎样才能打破这条线来摆脱PEP8警告而又不破坏代码呢?

python string pep8

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

PEP8 - 80个字符 - 大整数

这在某种程度上与关于大字符串和PEP8的问题有关.

如何使我的脚本具有符合PEP8的以下行("最大行长度"规则)?

pub_key = {
   'e': 3226833362680126101036263622033066816222202666130162062116461326212012222403311326222666622610430466620224662364142L,
   'n': 226421003861041248462826226103022608220328242204422684232640331238220232226321616266146243302342688266846281802662666622213868114632268211186223606846623310006662260110460620201618186828411322260686632603226636226662262862212140221422102106336342228236361106240226122644614266186283436228208626640846820224661642086022346422443282224682686612228404266842316822624342226666622264826123822122031361242246432886612624262663222232331438863220022020826266366016100422L
}
Run Code Online (Sandbox Code Playgroud)

python coding-style pep8

12
推荐指数
3
解决办法
758
查看次数

考虑到PEP-8处理长琴弦的最佳方法是什么?

结果字符串有一个换行符(我相信这个语法会破坏我的IDE中的函数折叠):

>>> def linebreak():
>>>     return """Hello this is a really long string which eventually
>>> needs to be breaked due to line length"""
>>> print(linebreak())
Hello this is a really long string which eventually
needs to be breaked
Run Code Online (Sandbox Code Playgroud)

这样更好,但这不仅包括换行符,还包括结果字符串中的多个空格:

>>> def whitespace():
>>>     some_string = """Hello this is a really long string which eventually
>>>                      needs to be breaked due to line length"""
>>> print(whitespace())
Hello this is a really long string which eventually
                     needs to be breaked
Run Code Online (Sandbox Code Playgroud)

正如 …

python

5
推荐指数
0
解决办法
493
查看次数