相关疑难解决方法(0)

如何在python中编写字符串文字而不必转义它们?

有没有办法在python中声明一个字符串变量,以便它内部的所有内容都自动转义,或者有它的文字字符值?

不是要问如何用斜线来逃避引号,这是显而易见的.我要求的是一个通用的方法,使字符串文字中的所有内容,以便我不必手动完成并转义非常大的字符串的所有内容.有人知道解决方案吗?谢谢!

python string escaping

85
推荐指数
4
解决办法
9万
查看次数

如何防止在Python中自动转义特殊字符

我正在编写一个Python脚本,它接受文件路径作为字符串,解析它们,附加一个命令名,然后构建一个列表,然后传递给它subprocess.Popen()执行.此脚本用于处理Unix和Windows文件路径,最终应在两个系统上运行.

当我在Unix下运行时,如果我给出一个无意中包含转义字符的Windows路径(例如\Users\Administrator\bin),Python会将嵌入式解释\b为退格字符.我想防止这种情况发生.

据我所知,没有函数或方法将字符串变量表示为原始字符串.该'r'修饰符仅适用于字符串常量.

到目前为止,我能得到的最接近的是:

winpath = "C:\Users\Administrator\bin" 
winpath = winpath.replace('\b','\\b')
winpathlist = winpath.split('\\') 
Run Code Online (Sandbox Code Playgroud)

此时,winpathlist应该包含['C:','Users','Administrator','bin'],而不是['C','Users','Administrator\x08in'].

我可以添加额外的呼叫winpath.replace()处理等逃跑我可能会- ,\a,\f,\n,,\r -但不是.\t\v\x

是否有更多的pythonic方式来做到这一点?

python escaping special-characters

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

由乳胶中的显示方程触发的文档字符串无效转义序列

我在 Python 中的函数的文档字符串中添加了乳胶数学表达式。它会触发错误“W605 无效转义序列”,从而破坏 flake8 检查。如何修复它?

"""
The test function is defined as:
\sin(\pi x)/x
"""
Run Code Online (Sandbox Code Playgroud)

我现在通过使用双斜杠解决了这个问题。

python flake8

7
推荐指数
1
解决办法
1460
查看次数

Python 3.5 中的注释给出了 unicode 错误

我正在使用 Spyder IDE,Python 3.5,它是 anaconda 发行版的一部分。下面给出了代码的前几行:

# -*- coding: utf-8 -*-
"""
Created on Tue Sep 20 16:22:40 2016

@author: pavan
This program reads csv file from the given directory .
The input directory for this is : "C:\Users\pavan\Documents\Python Scripts\EOD from Yahoo"
The output file is "comprehensive_trend.xlsx"

"""
import pdb
import pandas as pd
from datetime import date, datetime, timedelta
import os, glob
# Delarations
full_path = os.path.realpath(__file__)
current_directory = os.path.dirname(full_path)
directory = current_directory + "\\EOD from Yahoo\\"
#directory = …
Run Code Online (Sandbox Code Playgroud)

unicode python-2.7 python-3.5

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

在 python 文档字符串中编写双反斜杠

我正在为带有clear_stop_character函数的python包编写文档,其中用户可以在列表中提供额外的停止字符。在我写的文档中:

"""
stoplist (list, default empty): Accepts a list of extra stop characters,
            should escape special regex characters (e.g., stoplist=['\\*']).
"""
Run Code Online (Sandbox Code Playgroud)

对于用户来说,看到停止字符之前的双反斜杠至关重要。然而,help()构建包的结果显示:

"""
stoplist (list, default empty): Accepts a list of extra stop characters,
            should escape special regex characters (e.g., stoplist=['\*']).
"""
Run Code Online (Sandbox Code Playgroud)

所以,这会对用户产生误导。
顺便说一句,我没有根据之前的问题找到解决方案。
有任何想法吗?

python documentation docstring escaping

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