相关疑难解决方法(0)

为什么反斜杠会出现两次?

当我创建一个包含反斜杠的字符串时,它们会重复:

>>> my_string = "why\does\it\happen?"
>>> my_string
'why\\does\\it\\happen?'
Run Code Online (Sandbox Code Playgroud)

为什么?

python string escaping repr backslash

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

Python:如何在if语句中使用RegEx?

我有以下代码,它查看一个目录中的文件,并将包含某个字符串的文件复制到另一个目录中,但我尝试使用正则表达式,因为字符串可以是大写和小写,也可以是两者的混合.

在我尝试使用RegEx之前,这是有效的代码

import os
import re
import shutil

def test():
    os.chdir("C:/Users/David/Desktop/Test/MyFiles")
    files = os.listdir(".")
    os.mkdir("C:/Users/David/Desktop/Test/MyFiles2")
    for x in (files):
        inputFile = open((x), "r")
        content = inputFile.read()
        inputFile.close()
        if ("Hello World" in content)
            shutil.copy(x, "C:/Users/David/Desktop/Test/MyFiles2")
Run Code Online (Sandbox Code Playgroud)

当我尝试使用RegEx时,这是我的代码

import os
import re
import shutil

def test2():
    os.chdir("C:/Users/David/Desktop/Test/MyFiles")
    files = os.listdir(".")
    os.mkdir("C:/Users/David/Desktop/Test/MyFiles2")
    regex_txt = "facebook.com"
    for x in (files):
        inputFile = open((x), "r")
        content = inputFile.read()
        inputFile.close()
        regex = re.compile(regex_txt, re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)

我猜我需要一行代码

if regex = re.compile(regex_txt, re.IGNORECASE) == True
Run Code Online (Sandbox Code Playgroud)

但我似乎无法得到任何工作,如果有人能指出我正确的方向,将不胜感激.

python regex

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

'r'在python中的字符串之前表示什么?

我想理解为什么我们在python中的路径名之前使用ar,例如

dirname = r'C:\temp\parts'
Run Code Online (Sandbox Code Playgroud)

python python-2.7

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

Python正则表达式匹配整个单词

我无法为下面的场景找到正确的正则表达式:

让我们说:

a = "this is a sample"
Run Code Online (Sandbox Code Playgroud)

我希望匹配整个单词 - 例如,匹配"hi"应该返回False,因为"hi"它不是一个单词,并且"is"应该返回True,因为左侧和右侧没有字母字符.

python regex

33
推荐指数
2
解决办法
5万
查看次数

Python将字符串与正则表达式匹配

我需要一个python正则表达式来检查字符串中是否存在单词.该字符串可能以逗号分隔.

所以,例如,

line = 'This,is,a,sample,string'
Run Code Online (Sandbox Code Playgroud)

我想基于"样本"进行搜索,这将返回true.我很喜欢reg ex,所以当我查看python文档时,我看到了类似的内容

import re
re.match(r'sample', line)
Run Code Online (Sandbox Code Playgroud)

但我不知道为什么在文本匹配之前会有'r'.有人可以用正则表达式帮助我吗?

python regex

32
推荐指数
4
解决办法
14万
查看次数

在python中使用反斜杠(不要逃避)

import os
path= os.getcwd()
final= path +'\xulrunner.exe ' + path + '\application.ini'
print final
Run Code Online (Sandbox Code Playgroud)

我想要出局:

c:\ python25\xulrunner.exe c:\ python25\application.ini

我不希望反斜杠作为字符串工作,我的意思是不要让它逃脱或做任何特别的事情.但我得到一个错误

无效\ x转义

我如何使用'\'作为'\'而不是逃避?

python string backslash

23
推荐指数
3
解决办法
6万
查看次数

Python Raw Strings

我有一个字符串s,其内容是可变的.我想把它变成原始字符串.我该怎么做?

类似于r''方法的东西.

python string

22
推荐指数
7
解决办法
5万
查看次数

在python中用反斜杠拆分字符串

简单的问题,但我正在与它挣扎太多时间.基本上我想用\(反斜杠)分割一个字符串.

 a = "1\2\3\4"
Run Code Online (Sandbox Code Playgroud)

试图逃避反斜杠但它似乎不起作用:

 print(a.split('\'))
 print(a.split('"\"'))
 print(a.split('\\'))
 print(a.split('"\\"'))
Run Code Online (Sandbox Code Playgroud)

我想得到这个结果:

 ['1','2','3','4']
Run Code Online (Sandbox Code Playgroud)

提前谢谢了

python

21
推荐指数
2
解决办法
4万
查看次数

在Python中记录来自不同类的多个日志文件

我想编写一个使用Python日志记录的Python类.这个Python类将负责在init函数中创建具有给定名称的文件.

我想在两个或更多类中创建上述类的对象,并期望生成两个或文件.

我尝试编写这个类,但我无法创建多个文件.

任何人都可以指导我如何做到这一点?

我创建了以下类:

class Logger:
def __init__(self, log_filename = "test.log"):
    if not os.path.exists("LogFiles"):
        os.makedirs("LogFiles")
    self.Logger = logging.getLogger("main")
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s : %(message)s',
                        filename= log_filename,
                        filemode='w')           # change filemode to 'w' to overwrite file on each run

    consoleHandler = logging.StreamHandler()
    consoleHandler.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s - %(message)s')
    consoleHandler.setFormatter(formatter)
    logging.getLogger('').addHandler(consoleHandler)      # Add to the root logger
    self.Logger.info("Starting new logging sessions")


def writeToFile(self, line):
    if self.Logger.propagate == True:
        self.Logger.debug(line)

def closeFile(self):

    if self.Logger.propagate == True:
        self.Logger.propagate = False
Run Code Online (Sandbox Code Playgroud)

python logging

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

如何在R字符串中转义反斜杠

我正在使用R将字符串写入文件:

> x1="\\str"
> x2="\\\str"
Error: '\s' is an unrecognized escape in character string starting "\\\s"
> x2="\\\\str"
> write(file='test',c(x1,x2))
Run Code Online (Sandbox Code Playgroud)

当我打开名为的文件时test,我看到了这个:

\str
\\str
Run Code Online (Sandbox Code Playgroud)

如果我想得到一个包含5个反斜杠的字符串,我应该写10个反斜杠,像这样吗?

x="\\\\\\\\\\str" 
Run Code Online (Sandbox Code Playgroud)

file-io r escaping backslash

14
推荐指数
3
解决办法
4万
查看次数

标签 统计

python ×9

backslash ×3

regex ×3

string ×3

escaping ×2

file-io ×1

logging ×1

python-2.7 ×1

r ×1

repr ×1