相关疑难解决方法(0)

"u"和"r"字符串标志究竟做了什么,以及什么是原始字符串文字?

在问这个问题时,我意识到我对原始字符串知之甚少.对于那些自称是Django训练师的人来说,这很糟糕.

我知道编码是什么,而且我知道u''自从我得到什么是Unicode以来我们独自做了什么.

  • 但到底r''做了什么呢?它会产生什么样的字符串?

  • And above all, what the heck does ur'' do?

  • Finally, is there any reliable way to go back from a Unicode string to a simple raw string?

  • Ah, and by the way, if your system and your text editor charset are set to UTF-8, does u'' actually do anything?

python unicode python-2.x rawstring

600
推荐指数
7
解决办法
41万
查看次数

是否值得使用Python的re.compile?

在Python中使用正则表达式编译有什么好处吗?

h = re.compile('hello')
h.match('hello world')
Run Code Online (Sandbox Code Playgroud)

VS

re.match('hello', 'hello world')
Run Code Online (Sandbox Code Playgroud)

python regex

422
推荐指数
13
解决办法
24万
查看次数

如何在string.replace中输入正则表达式?

我需要一些关于声明正则表达式的帮助.我的输入如下:

this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-100</[99>. 
and there are many other lines in the txt files
with<[3> such tags </[3>
Run Code Online (Sandbox Code Playgroud)

所需的输出是:

this is a paragraph with in between and then there are cases ... where the number ranges from 1-100. 
and there are many other lines in the txt files
with such tags
Run Code Online (Sandbox Code Playgroud)

我试过这个:

#!/usr/bin/python
import os, sys, re, glob
for infile in glob.glob(os.path.join(os.getcwd(), '*.txt')):
    for …
Run Code Online (Sandbox Code Playgroud)

python regex string replace

272
推荐指数
6
解决办法
29万
查看次数

在Python中搜索并替换文件中的一行

我想循环遍历文本文件的内容,并在某些行上进行搜索和替换,并将结果写回文件.我可以先将整个文件加载到内存中然后再写回来,但这可能不是最好的方法.

在以下代码中,最好的方法是什么?

f = open(file)
for line in f:
    if line.contains('foo'):
        newline = line.replace('foo', 'bar')
        # how to write this newline back to the file
Run Code Online (Sandbox Code Playgroud)

python file

271
推荐指数
11
解决办法
42万
查看次数

如何在python中用一个单词替换多个单词?

我有一些字符串可能包含某事物的缩写或全名,我想将它们全部替换为该单词的相同变体。

例如,

“8 gigs”、“8 GB”和“8 GBs”应全部更改为“8 GB”

最好的方法是什么?每个人都有单独的替换吗?

另外,我试图对多个单词(即兆字节、太字节)执行此操作,每个单词是否都需要不同的替换,或者是否有一种方法将它们全部放在一个单词中?

python string replace

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

标签 统计

python ×5

regex ×2

replace ×2

string ×2

file ×1

python-2.x ×1

rawstring ×1

unicode ×1