函数中Python多行字符串的正确缩进是什么?
def method():
string = """line one
line two
line three"""
Run Code Online (Sandbox Code Playgroud)
要么
def method():
string = """line one
line two
line three"""
Run Code Online (Sandbox Code Playgroud)
或者是其他东西?
在第一个例子中将字符串悬挂在函数外部看起来有点奇怪.
由于PEP8建议保持低于你的python程序的80列规则,我怎么能遵守长字符串,即
s = "this is my really, really, really, really, really, really, really long string that I'd like to shorten."
Run Code Online (Sandbox Code Playgroud)
我将如何将其扩展到以下行,即
s = "this is my really, really, really, really, really, really" +
"really long string that I'd like to shorten."
Run Code Online (Sandbox Code Playgroud) 我想在多个行中分割R脚本中的一行(因为它太长了).我怎么做?
具体来说,我有一条线,如
setwd('~/a/very/long/path/here/that/goes/beyond/80/characters/and/then/some/more')
Run Code Online (Sandbox Code Playgroud)
是否有可能将长路径分成多条线?我试过了
setwd('~/a/very/long/path/here/that/goes/beyond/80/characters/and/
then/some/more')
Run Code Online (Sandbox Code Playgroud)
用return键在第一行的末尾; 但这不起作用.
谢谢.
这就是我所知道的如何编写和保存它
Html_file= open"(filename","w")
Html_file.write()
Html_file.close
Run Code Online (Sandbox Code Playgroud)
但是,如果我想写一个非常长的代码,我如何保存到文件:
1 <table border=1>
2 <tr>
3 <th>Number</th>
4 <th>Square</th>
5 </tr>
6 <indent>
7 <% for i in range(10): %>
8 <tr>
9 <td><%= i %></td>
10 <td><%= i**2 %></td>
11 </tr>
12 </indent>
13 </table>
Run Code Online (Sandbox Code Playgroud) 我有一个很长的正则表达式,我想继续到下一行,但我尝试的一切给了我一个EOL或打破正则表达式.我已经在括号内继续了一行,并阅读了这一点,除此之外,我如何在Python中进行换行(换行)?
工作,但仍然太长:
REGEX = re.compile(
r'\d\s+\d+\s+([A-Z0-9-]+)\s+([0-9]+.\d\(\d\)[A-Z0-9]+)\s+([a-zA-Z\d-]+)')
Run Code Online (Sandbox Code Playgroud)
错误:
REGEX = re.compile(
r'\d\s+\d+\s+([A-Z0-9-]+)\s+([0-9]+.\d\(\d\)[A-Z0-9]+
)\s+([a-zA-Z\d-]+)')
SyntaxError: EOL while scanning string literal
REGEX = re.compile(
r'\d\s+\d+\s+([A-Z0-9-]+)\s+([0-9]+.\d\(\d\
)[A-Z0-9]+)\s+([a-zA-Z\d-]+)')
sre_constants.error: unbalanced parenthesis
REGEX = re.compile(
r'\d\s+\d+\s+([A-Z0-9-]+)\s+( \
[0-9]+.\d\(\d\)[A-Z0-9]+)\s+([a-zA-Z\d-]+)')
regex no longer works
REGEX = (re.compile(
r'\d\s+\d+\s+([A-Z0-9-]+)\s+(
[0-9]+.\d\(\d\)[A-Z0-9]+)\s+([a-zA-Z\d-]+)'))
SyntaxError: EOL while scanning string literal
Run Code Online (Sandbox Code Playgroud)
我已经能够缩短我的正则表达式,所以这不再是一个问题,但我现在有兴趣知道如何使用长正则表达式进行行继续?
三重行情
'''
This is a
multi-line
string.
'''
Run Code Online (Sandbox Code Playgroud)
串联
('this is '
'a string')
Run Code Online (Sandbox Code Playgroud)
逃跑
'This is'\
'a string'
Run Code Online (Sandbox Code Playgroud)
我也知道在字符串前面加上前缀r会使它成为原始字符串,对文件路径很有用。
r'C:\Path\To\File'
Run Code Online (Sandbox Code Playgroud)但是,我有一个很长的文件路径,它跨越多行并且需要是一个原始字符串。我该怎么做呢?
这有效:
In [1]: (r'a\b'
...: '\c\d')
Out[1]: 'a\\b\\c\\d'
Run Code Online (Sandbox Code Playgroud)
但出于某种原因,这不会:
In [4]: (r'on\e'
...: '\tw\o')
Out[4]: 'on\\e\tw\\o'
Run Code Online (Sandbox Code Playgroud)
为什么"t"只有一个反斜杠?
我有一段文字:
没有你那张聪明的嘴,我该怎么办?
拉我进去,你
把我踢出去你让我 头晕目眩,别开玩笑了,我不能把你
固定在那个美丽的头脑里发生了什么
我在你神奇的神秘之旅中
我很头晕,不要不知道是什么打击了我,但我会没事的
...现在我想用这个文本创建一个字符串。例如在 Python 中:
string = " What would I do without your smart mouth?
Drawing me in, and you kicking me out
You've got my head spinning, no kidding, I can't pin you down
What's going on in that beautiful mind
I'm on your magical mystery ride
And I'm so dizzy, don't know what hit me, but I'll be alright "
Run Code Online (Sandbox Code Playgroud)
但是 Python 没有将其视为字符串,因为它包含换行符。如何将此文本作为字符串?
结果字符串有一个换行符(我相信这个语法会破坏我的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)
正如 …
我有一个很长的字符串,我想用代码保存在一个文本文件中:
taxtfile.write(a)
但由于字符串太长,保存的文件打印为:
"something something ..... something something"
Run Code Online (Sandbox Code Playgroud)
我如何确保它会保存整个字符串而不截断它?