小编use*_*957的帖子

python .replace()正则表达式

我试图在"</ html>"标记之后抓取所有内容并将其删除,但我的代码似乎没有做任何事情.难道.replace()不支持正则表达式?

z.write(article.replace('</html>.+', '</html>'))
Run Code Online (Sandbox Code Playgroud)

python regex

188
推荐指数
4
解决办法
27万
查看次数

python仅限首字母大写

我知道.capitalize()将字符串的第一个字母大写,但如果第一个字符是整数呢?

这个

1bob
5sandy
Run Code Online (Sandbox Code Playgroud)

对此

1Bob
5Sandy
Run Code Online (Sandbox Code Playgroud)

python letter capitalize

163
推荐指数
6
解决办法
19万
查看次数

python正则表达式匹配任何东西

我的正则表达式对我的字符串没有做任何事情.

蟒蛇

data = 'random\n<article stuff\n</article>random stuff'
datareg = re.sub(r'.*<article(.*)</article>.*', r'<article\1</article>', data, flags=re.MULTILINE)
print datareg
Run Code Online (Sandbox Code Playgroud)

我明白了

random
<article stuff
</article>random stuff
Run Code Online (Sandbox Code Playgroud)

我想要

<article stuff
</article>
Run Code Online (Sandbox Code Playgroud)

python regex

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

django简单的ajax请求

我正在尝试向我的views.py发送ajax请求,但我不知道如何使用该路径.我的观点位于我的服务器/home/pycode/main/main/apps/builder/views.py. 上我发送请求的页面位于/home/dbs/www/python.html 我是否需要向我的urls.py添加内容?

views.py

#!/usr/bin/env python26
from django.http import HttpResponse
def main(request):
    return HttpResponse("from python with love")
Run Code Online (Sandbox Code Playgroud)

python.html jquery ajax

<script language="JavaScript">
$(document).ready(function() {
$("#myform").submit(function() {

    var myCheckboxes = new Array();
    $("input:checked").each(function() {
       myCheckboxes.push($(this).val());
    });
    $.ajax({
        type: "POST",
        url: '/main',
        data: { myCheckboxes:myCheckboxes },
        success: function(response){
            alert(response);
        }
    });
    return false;
});
});
</script>
Run Code Online (Sandbox Code Playgroud)

python django jquery

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

在'\ id'之后抓取字符串中的第一个单词

如何'\id '在字符串后面抓住第一个单词?

串:

'\id hello some random text that can be anything'
Run Code Online (Sandbox Code Playgroud)

蟒蛇

for line in lines_in:
    if line.startswith('\id '):
        book = line.replace('\id ', '').lower().rstrip()
Run Code Online (Sandbox Code Playgroud)

我得到了什么

book = 'hello some random text that can be anything'
Run Code Online (Sandbox Code Playgroud)

我想要的是

book = 'hello'
Run Code Online (Sandbox Code Playgroud)

python regex

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

python迭代字符串

我正在将"文章"的内容写入文本文件.

源文件:

lol hi
lol hello
lol text
lol test
Run Code Online (Sandbox Code Playgroud)

蟒蛇:

for line in all_lines:
    if line.startswith('lol '):
        mystring = line.replace('lol ', '').lower().rstrip()

article = 'this is my saved file\n' + mystring + '\nthe end'
Run Code Online (Sandbox Code Playgroud)

这是保存到txt文件的内容:

this is my saved file
test
the end
Run Code Online (Sandbox Code Playgroud)

这是我想要保存到txt文件的内容:

this is the saved file
hi
hello
test
text
the end
Run Code Online (Sandbox Code Playgroud)

python string file

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

python循环不添加1

无论我尝试什么,"我"总是保持为"1".每次进入文件夹中包含"1Chr"的下一个文件时,我正在尝试向"i"添加"1".

蟒蛇

for name in glob.glob('*.html'):
  i = 1
  with open(name) as k:
    content = k.read()
    if '1Chr.'+str(i)+'.' in name:
      book = name.split('.')[0].upper().rstrip()
      x=open('final/'+book+'.SFM', 'a')
      x.write(content)
      i += 1
      x.close()    
Run Code Online (Sandbox Code Playgroud)

python

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

python比萨utf8问题

我正在尝试将本地HTML转换为PDF,但是html文档中的非ASCII字符最终在PDF中损坏。为什么pisa不能对所有UTF-8字符都起作用?

with open('file.html') as m:
  data = m.read()
  m.close()        
  pisa.CreatePDF(data, file('final.pdf', 'w'))
Run Code Online (Sandbox Code Playgroud)

python pdf utf-8 pisa

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

python re.sub正则表达式

Python不断返回一个字符损坏的字符串.

蟒蛇

test = re.sub('handle(.*?)', '<verse osisID="lol">\1</verse>', 'handle a bunch of random text here.')
print test
Run Code Online (Sandbox Code Playgroud)

我想要的是

<verse osisID="lol">a bunch of random text here.</verse>
Run Code Online (Sandbox Code Playgroud)

我得到了什么

<verse osisID="lol">*broken character*</verse>a bunch of random text here.
Run Code Online (Sandbox Code Playgroud)

python regex

0
推荐指数
1
解决办法
491
查看次数

标签 统计

python ×9

regex ×4

capitalize ×1

django ×1

file ×1

jquery ×1

letter ×1

pdf ×1

pisa ×1

string ×1

utf-8 ×1