python 将一个文件中内容添加到另一个文件指定位置

本文介绍如何使用Python脚本将文件内容添加到HTML文件的特定位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目的:将文件test.txt内容添加到文件test.html指定字符串的前面

文件内容:

    test.txt内容如下:

  

  <tr>
    <td>10.1</td><td>10.2</td><td>10.3</td><td>10.4</td><td>10.5</td>
<td>10.6</td><td>10.7</td><td>10.8</td><td>10.9</td><td>10.10</td><td>10.11</td><td>10.12</td><td>10.13</td><td>10.14</td><td>10.15</td><td>10.16</td><td>10.17</td><td>10.18</td><td>10.19</td><td>10.20</td><td>10.21</td><td>10.22</td>
  </tr>

  text.html内容如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<table width="200" border="1">
  <tr>
    <td>Q1</td><td>Q2</td><td>Q3</td><td>Q4</td><td>Q5</td>	<td>Q6</td><td>Q7</td><td>Q8</td><td>Q9</td><td>Q10</td>
	<td>Q11</td><td>Q12</td><td>Q13</td><td>Q14</td><td>Q15</td><td>Q16</td><td>Q17</td><td>Q18</td><td>Q19</td><td>Q20</td>
	<td>Q21</td><td>Q22</td>
  </tr>
  <tr>
    <td>10.1</td><td>10.2</td><td>10.3</td><td>10.4</td><td>10.5</td>	<td>10.6</td><td>10.7</td><td>10.8</td><td>10.9</td><td>10.10</td><td>10.11</td><td>10.12</td><td>10.13</td><td>10.14</td><td>10.15</td><td>10.16</td><td>10.17</td><td>10.18</td><td>10.19</td><td>10.20</td><td>10.21</td><td>10.22</td>
  </tr>
 
</table>
</body>
</html>

执行的python脚本

#!/usr/local/python
# coding=UTF-8
import os

file = open( "test.html", "r" )
fileadd = open("test.txt","r")
content = file.read()
contentadd = fileadd.read()
file.close()
fileadd.close()
pos = content.find( "</table>" )
if pos != -1:
        content = content[:pos] + contentadd + content[pos:]
        file = open( "test.html", "w" )
        file.write( content )
        file.close()
        print "OK"





### Python 使用 Pandas 处理 Excel 文件 为了实现将一个 Excel 文件中的指定数据添加一个 Excel 文件指定列中,可以按照以下方法操作: #### 方法概述 首先导入必要的库 `pandas` 和定义两个 Excel 文件路径。接着分别读取这两个文件至不同的 DataFrame 对象中。通过选取源文件特定列的数据并将其赋值给目标文件相应位置来完成数据迁移。最后保存更新后的目标文件。 #### 实现代码示例 ```python import pandas as pd # 定义源文件和目标文件路径 source_file_path = 'D:\\William\\Projects\\python\\获取股票数据\\原始数据.xlsx' target_file_path = 'D:\\William\\Projects\\python\\获取股票数据\\目标数据.xlsx' # 读取源文件和目标文件 df_source = pd.read_excel(source_file_path) # 源文件DataFrame对象[^1] df_target = pd.read_excel(target_file_path) # 目标文件DataFrame对象 # 假设要从源文件复制 "A" 列的内容到目标文件的 "B" 列 column_to_copy_from = 'A' # 要复制的列名称 column_to_paste_into = 'B' # 要粘贴的目标列名称 # 确认两者的长度相同以便直接对应赋值;如果不同,则需考虑如何处理差异部分 if len(df_source) != len(df_target): raise ValueError("Source and target DataFrames must have the same length.") # 执列间数据转移 df_target[column_to_paste_into] = df_source[column_to_copy_from][^2] # 将更改写回目标文件而不显示索引列 df_target.to_excel(target_file_path, index=False)[^3] ``` 此段脚本展示了怎样利用 Pandas 库高效地管理多个 Excel 表格之间的交互,并确保了良好的兼容性和易用性[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值