Kar*_*tel 45
原始字符串不是一种不同的字符串.它们是在源代码中描述字符串的不同方式.一旦创建了字符串,它就是它.
Jol*_*234 35
我相信你要找的是str.encode("string-escape")函数.例如,如果您有一个想要'raw string'的变量:
a = '\x89'
a.encode('unicode_escape')
'\\x89'
Run Code Online (Sandbox Code Playgroud)
注意:string-escape用于python 2.x及更早版本
我正在寻找类似的解决方案,并通过以下方式找到解决方案: cast raw strings python
sla*_*der 32
由于 Python 中的字符串是不可变的,因此您不能“使它”有任何不同。但是,您可以从 中创建一个新的原始字符串s,如下所示:
raw_s = r'{}'.format(s)
Sin*_*ion 19
原始字符串仅适用于字符串文字.它们存在,以便您可以更方便地表达将通过转义序列处理修改的字符串.在字符串文字中写出正则表达式或其他形式的代码时,这尤其有用.如果您想在不转义处理unicode字符串,只需使用前缀ur,像ur'somestring'.
dhe*_*inz 13
从 Python 3.6 开始,您可以使用以下内容(类似于 @slashCoder):
def to_raw(string):
return fr"{string}"
my_dir ="C:\data\projects"
to_raw(my_dir)
Run Code Online (Sandbox Code Playgroud)
产量'C:\\data\\projects'。我在 Windows 10 机器上使用它来将目录传递给函数。
对于Python 3,顺便可以做到这一点不添加双反斜线和简单的蜜饯\n,\t等是:
a = 'hello\nbobby\nsally\n'
a.encode('unicode-escape').decode().replace('\\\\', '\\')
print(a)
Run Code Online (Sandbox Code Playgroud)
这给出了一个可以写为 CSV 的值:
hello\nbobby\nsally\n
Run Code Online (Sandbox Code Playgroud)
其他特殊字符似乎没有解决方案,但是,在它们之前可能会有一个 \ 。这是一个无赖。解决这个问题会很复杂。
例如,要将pandas.Series包含具有特殊字符的字符串列表的 a序列化为BERT期望的格式的文本文件,每个句子之间有一个 CR,每个文档之间有一个空行:
with open('sentences.csv', 'w') as f:
current_idx = 0
for idx, doc in sentences.items():
# Insert a newline to separate documents
if idx != current_idx:
f.write('\n')
# Write each sentence exactly as it appared to one line each
for sentence in doc:
f.write(sentence.encode('unicode-escape').decode().replace('\\\\', '\\') + '\n')
Run Code Online (Sandbox Code Playgroud)
此输出(对于标记为句子的所有语言的 Github CodeSearchNet 文档字符串):
Makes sure the fast-path emits in order.
@param value the value to emit or queue up\n@param delayError if true, errors are delayed until the source has terminated\n@param disposable the resource to dispose if the drain terminates
Mirrors the one ObservableSource in an Iterable of several ObservableSources that first either emits an item or sends\na termination notification.
Scheduler:\n{@code amb} does not operate by default on a particular {@link Scheduler}.
@param the common element type\n@param sources\nan Iterable of ObservableSource sources competing to react first.
A subscription to each source will\noccur in the same order as in the Iterable.
@return an Observable that emits the same sequence as whichever of the source ObservableSources first\nemitted an item or sent a termination notification\n@see ReactiveX operators documentation: Amb
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48985 次 |
| 最近记录: |