私信  •  关注

Tomalak

Tomalak 最近创建的主题
Tomalak 最近回复了
4 年前
回复了 Tomalak 创建的主题 » 使用jQuery替换元素
  • 要搜索现有元素,请在jQuery中使用CSS表达式 $() .
  • 要创建新元素,请在jQuery中使用HTML代码 $() .
  • .replaceAll() 是一个字符串函数。你是说 .replaceWith() .

$("font[size=3]").replaceWith( $("<div class='legend well'>") );

或者更短

$("font[size=3]").replaceWith("<div class='legend well'>");

只交换 <font> 元素 没有 此外,更换内容还需要几个步骤。

$("font[size=3]").each(function () {
  // insert new container div after each `<font>`
  var $div = $("<div class='legend well'>").insertAfter(this);

  // remove the `<font>` and append its children to the new container
  $(this).remove().children().appendTo($div);
});
div.legend {
   color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<font size="3">
  <p>elements to keep</p>
</font>
<font size="3">
  <p>more elements to keep</p>
</font>
4 年前
回复了 Tomalak 创建的主题 » 分裂Python中的txt文件行
with open('path/snip.txt', 'r') as f_input, open('path/snip_output.txt', 'w') as f_output:
    for line in f_input:
        xyz = line.split()
        x, y, z = xyz[0:3], xyz[3:6], xyz[6:9]
        for triple in zip(x, y, z):
            f_output.write(' '.join(triple) + '\n')

1.9849207e-01 9.6169322e-02 1.5000000e-02
1.9993099e-01 9.6354487e-02 1.6090730e-02
2.0150793e-01 1.0630896e-01 1.5000000e-02
1.9993099e-01 9.6354487e-02 1.6090730e-02
2.0261176e-01 1.0536750e-01 1.6090730e-02
2.0150793e-01 1.0630896e-01 1.5000000e-02
7 年前
回复了 Tomalak 创建的主题 » 用python重建sql server文件

的价值 FILE_CONTENT 是base64编码的。这意味着它是一个由64个可能的字符组成的字符串,这些字符表示原始字节。您只需要对字符串进行base64解码,并将结果字节直接写入文件。

import base64

content_str = "H4sIAAAAAAAAAOy8VXQcy7Ku22JmZmZmspiZGS2WLGa0xc=="

with open(os.path.expanduser('test.pdf'), 'wb') as fp:
    fp.write(base64.b64decode(content_str))

Base64序列 "H4sI" 在内容字符串的开头转换为字节 0x1f 我是说, 0x8b 我是说, 0x08 .这些字节通常不在pdf文件的开头,而是表示gzip压缩数据流。有可能pdf阅读器无法理解这一点。

我不确定gzip压缩是否是pdf文件格式的有效部分,但它是web通信的有效部分,所以可能文件流是为了传输/下载而压缩的,在将其写入数据库之前没有解压缩。

如果PDF阅读器不接受数据,请在将其保存到文件之前将其解压缩:

import gzip

# ...

with open(os.path.expanduser('test.pdf'), 'wb') as fp:
    fp.write(gzip.decompress(base64.b64decode(content_str)))