免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9729 | 回复: 14
打印 上一主题 下一主题

如何编写Python脚本替换文件中的多行字符? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-08-25 23:33 |只看该作者 |倒序浏览
20可用积分
如题,我只要替换掉下面的版权信息,为“xxx”该如何编写Python脚本呢?请指教,不甚感激,我可以另外开贴追加200分
/*
  *   Copyright   2002   Sun   Microsystems,   Inc.   All   rights   reserved.
  *
  *   Redistribution   and   use   in   source   and   binary   forms,   with   or   without
  *   modification,   are   permitted   provided   that   the   following   conditions
  *   are   met:
  *
  *   -   Redistributions   of   source   code   must   retain   the   above   copyright
  *       notice,   this   list   of   conditions   and   the   following   disclaimer.
  *
  *   -   Redistribution   in   binary   form   must   reproduce   the   above   copyright
  *       notice,   this   list   of   conditions   and   the   following   disclaimer   in
  *       the   documentation   and/or   other   materials   provided   with   the
  *       distribution.
  *
  *   Neither   the   name   of   Sun   Microsystems,   Inc.   or   the   names   of
  *   contributors   may   be   used   to   endorse   or   promote   products   derived
  *   from   this   software   without   specific   prior   written   permission.
  *
  *   This   software   is   provided   "AS   IS, "   without   a   warranty   of   any
  *   kind.   ALL   EXPRESS   OR   IMPLIED   CONDITIONS,   REPRESENTATIONS   AND
  *   WARRANTIES,   INCLUDING   ANY   IMPLIED   WARRANTY   OF   MERCHANTABILITY,
  *   FITNESS   FOR   A   PARTICULAR   PURPOSE   OR   NON-INFRINGEMENT,   ARE   HEREBY
  *   EXCLUDED.   SUN   AND   ITS   LICENSORS   SHALL   NOT   BE   LIABLE   FOR   ANY   DAMAGES
  *   SUFFERED   BY   LICENSEE   AS   A   RESULT   OF   USING,   MODIFYING   OR
  *   DISTRIBUTING   THE   SOFTWARE   OR   ITS   DERIVATIVES.   IN   NO   EVENT   WILL   SUN
  *   OR   ITS   LICENSORS   BE   LIABLE   FOR   ANY   LOST   REVENUE,   PROFIT   OR   DATA,   OR
  *   FOR   DIRECT,   INDIRECT,   SPECIAL,   CONSEQUENTIAL,   INCIDENTAL   OR
  *   PUNITIVE   DAMAGES,   HOWEVER   CAUSED   AND   REGARDLESS   OF   THE   THEORY   OF
  *   LIABILITY,   ARISING   OUT   OF   THE   USE   OF   OR   INABILITY   TO   USE   SOFTWARE,
  *   EVEN   IF   SUN   HAS   BEEN   ADVISED   OF   THE   POSSIBILITY   OF   SUCH   DAMAGES.
  *
  *   You   acknowledge   that   Software   is   not   designed,   licensed   or   intended
  *   for   use   in   the   design,   construction,   operation   or   maintenance   of
  *   any   nuclear   facility.
  */

/*
*test
*/
aaaaaaaaaab
skldjfklsdjlkfj

最佳答案

查看完整内容

我将你的内容存成了source.c,读文件然后输出,当然你也可以输出到文件,这个你自己后期操作,代码如下:运行结果如下:根据是否在代码注释内以及代码注释内第一行是否有copyright字样判定,比较粗糙,但是应该满足你要求了吧

论坛徽章:
0
2 [报告]
发表于 2010-08-25 23:33 |只看该作者
我将你的内容存成了source.c,读文件然后输出,当然你也可以输出到文件,这个你自己后期操作,代码如下:
  1. f = open('source.c')
  2. in_comment = is_copyright = False
  3. for i in f:
  4.         t = i.split()
  5.         if len(t) == 0:
  6.                 print i,
  7.                 continue
  8.         if in_comment:
  9.                 if t[0] == '*/':
  10.                         in_comment = False
  11.                         print i,
  12.                 elif not is_copyright:
  13.                         if t[1] == 'Copyright':
  14.                                 is_copyright = True
  15.                                 print 'xxx'
  16.                         else:
  17.                                 print i,
  18.         else:
  19.                 if t[0] == '/*':
  20.                         in_comment = True
  21.                 print i,
复制代码
运行结果如下:
  1. /*
  2. xxx
  3.   */

  4. /*
  5. */
  6. aaaaaaaaaab
  7. skldjfklsdjlkfj
复制代码
根据是否在代码注释内以及代码注释内第一行是否有copyright字样判定,比较粗糙,但是应该满足你要求了吧

论坛徽章:
0
3 [报告]
发表于 2010-08-26 00:22 |只看该作者
打开文件,用正则匹配版权信息(/* */),找到需要删除的起止行号(一般都是[1,x]行吧,记住这个x),然后用个临时文件中转一下

论坛徽章:
0
4 [报告]
发表于 2010-08-26 09:41 |只看该作者
本帖最后由 t6760915 于 2010-08-26 09:43 编辑
  1. result = ''
  2. newstr = str.split('/*')
  3. result += newstr[0]
  4. result += newstr[1].split('*/')[1]
复制代码
这个只能做文本中含一处/*ssss*/的情况。

论坛徽章:
0
5 [报告]
发表于 2010-08-26 10:16 |只看该作者
本帖最后由 月吻清风 于 2010-08-26 10:19 编辑

正则

论坛徽章:
0
6 [报告]
发表于 2010-08-26 10:17 |只看该作者
  1. #!/usr/bin/env python
  2. import os,re

  3. f = file("test","r").read()
  4. pat = re.compile(r'/\*(.*?)\*/',flags=20)
  5. count = re.sub(pat,'xxx',f)
  6. print count
复制代码

论坛徽章:
0
7 [报告]
发表于 2010-08-26 11:22 |只看该作者
月吻清风 发表于 2010-08-26 10:17



    这个跟我写的一样

论坛徽章:
0
8 [报告]
发表于 2010-08-26 22:39 |只看该作者
回复 2# insnowind


    这个我也会说,但我不会写,以前完全没有用过Python

论坛徽章:
0
9 [报告]
发表于 2010-08-26 22:40 |只看该作者
回复 5# 月吻清风


    能给点思路解析,注释吗?thx

论坛徽章:
0
10 [报告]
发表于 2010-08-26 22:43 |只看该作者
回复 6# daybreakcx


    如果到星期天还没有更好的回答的话,分全给你。
    不过就算有比这更好的答案也会给你20分。
    设置一个悬赏最多只能是20分吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP