关于将项目从一个文件移到另一个文件的问题 [英] newbe question about removing items from one file to another file

查看:68
本文介绍了关于将项目从一个文件移到另一个文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def simplecsdtoorc(filename):

file = open(filename," r")

alllines = file.read_until("< / CsInstruments>" ;)

pattern1 = re.compile("< /")

orcfilename = filename [-3:] +" orc"

for line in alllines:

if if not pattern1

print>> orcfilename,line


我很漂亮确定我的代码并不接近我想要的。我需要能够

跳过html,如命令从< definedto< undefinedand to key on

另一个单词加入< / CsInstrumentsto结束例程


我也在看se 2.2 beta但是没有看到任何简单的方法来使用它

这个或者那个问题搜索并替换我可能的地方只需添加

它作为一个菜单项而不用担心它。


感谢提前任何帮助

解决方案

听起来你需要使用html解析器,请在

文档中查看....


< Er ********* @ msn.com写信息

新闻:11 ****************** ****@i3g2000cwc.googlegro ups.com ...


def simplecsdtoorc(filename):

file = open(filename, " r")

alllines = file.read_until("< / CsInstruments>")

pattern1 = re.compile("< /" )

orcfilename = filename [-3:] +" orc"

for line in alllines:

if if not pattern1

print>> orcfilename,line


我很确定我的代码并不接近我想要的。我需要能够

跳过html,如命令从< definedto< undefinedand to key on

另一个单词加入< / CsInstrumentsto结束例程


我也在看se 2.2 beta但是没有看到任何简单的方法来使用它

这个或者那个问题搜索并替换我可能的地方只需添加

它作为一个菜单项而不用担心它。


感谢提前任何帮助




PetDragon写道:


听起来你需要使用html解析器,检查一下在

文件中....


< Er ********* @ msn.comwrote in message

news:11 ********************** @ i3g2000cwc.googlegro ups.com ...


def simplecsdtoorc(filename):

file = open(filename," r")

alllines = file.read_until("< / CsInstr uments>")

pattern1 = re.compile("< /")

orcfilename = filename [-3:] +" orc"

for line in alllines:

if if not pattern1

print>> orcfilename,line


我很确定我的代码并不接近我想要的。我需要能够

跳过html,如命令从< definedto< undefinedand to key on

另一个单词加入< / CsInstrumentsto结束例程


我也在看se 2.2 beta但是没有看到任何简单的方法来使用它

这个或者那个问题搜索并替换我可能的地方只需添加

它作为一个菜单项而不用担心它。


感谢提前任何帮助



我会稍微调查一下因为那样的html就像......也许

一些例子可以引导我在很多方面正确的方向..

http://www.dexrow.com


Er ******** *@msn.com 写道:


def simplecsdtoorc(filename):

file = open(filename," r" ; )

alllines = file.read_until("< / CsInstruments>")

pattern1 = re.compile("< /")

orcfilename = filename [-3:] +" orc"

for line in alllines:

if if not pattern1

print>> orcfilename,line


我很确定我的代码并不接近我想要的。我需要能够

跳过html,如命令从< definedto< undefinedand to key on

另一个单词加入< / CsInstrumentsto结束例程


我也在看se 2.2 beta但是没有看到任何简单的方法来使用它

这个或者那个问题搜索并替换我可能的地方只需添加

它作为一个菜单项而不用担心它。


感谢提前任何帮助



如果您正在处理html或类似html的文件,请查看

beautifulsoup。我有理由在前几天使用它而男人是否有用

有用!


同时,您发布的代码有一些小问题:


1)open()默认为''r'',你可以在调用open()时将其保留为

读取文件。


2)''file''是一个内置类型(它是由

open()返回的文件对象的类型)所以你不应该'不要将它用作变量名。


3)文件对象没有read_until()方法。你可以说

类似于:


f = open(文件名)

lines = []

for the line in f:

lines.append(line)

如果''< / CsInstruments>''排成一行:

break


4)filename [-3:]将为你提供文件名中的最后3个字符。我猜你想要除了最后3个字符之外的所有字符,那个文件名是[: - 3],

但是请看os.path.splitext ()函数,以及os.path中的其他

函数:
http://docs.python.org/lib/module-os.path.html


5) re.compile()返回的正则表达式对象将始终

评估为True,因此您要将数据上的search()方法调用为

搜索:


如果不是pattern1.search(行):


但是,6)使用re作为简单的模式< /"是有点矫枉过正了。只需

使用''in''或字符串的find()方法:


if"< /"不符合:


或:


pos = line.find("< /")

if pos == -1:

print>> orcfilename,line

else:

print>> orcfilename, line [:pos]


7)print>文件用法需要一个文件(或类文件对象,

任何带有write()方法的东西,我认为)不是字符串。你需要像这样使用




orcfile = open(orcfilename,''w'')

#。 ..

打印> orcfile,行

8)如果你有一个行列表,你可以使用writelines()

一次性写入文件的方法:


open(orcfilename,''w'')。writelines(lines)


当然使用

find()从上一行中删除不需要的数据,如上所示。


我希望这有帮助。


查看文件对象上的文档:
http://docs.python.org/lib/bltin-file-objects.html ,但就像我说的那样,

如果你正在交易使用html或类似html的文件,请务必查看

beautifulsoup。另外,还有用于解析XML

的elementtree包也可以在这里提供帮助。


~Simon


def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn''t close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn''t see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance

解决方案

Sounds like you need to use html parser, check it out in the
documentation....

<Er*********@msn.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...

def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn''t close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn''t see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance




PetDragon wrote:

Sounds like you need to use html parser, check it out in the
documentation....

<Er*********@msn.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...

def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn''t close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn''t see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance

I will look into that a little bit since that is so html like... maybe
some of the examples can lead me in the right direction on alot of it..

http://www.dexrow.com


Er*********@msn.com wrote:

def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename[-3:] + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line

I am pretty sure my code isn''t close to what I want. I need to be able
to skip html like commands from <definedto <undefinedand to key on
another word in adition to </CsInstrumentsto end the routine

I was also looking at se 2.2 beta but didn''t see any easy way to use it
for this or for that matter search and replace where I could just add
it as a menu item and not worry about it.

thanks for any help in advance

If you''re dealing with html or html-like files, do check out
beautifulsoup. I had reason to use it the other day and man is it ever
useful!

Meantime, there are a few minor points about the code you posted:

1) open() defaults to ''r'', you can leave it out when you call open() to
read a file.

2) ''file'' is a builtin type (it''s the type of file objects returned by
open()) so you shouldn''t use it as a variable name.

3) file objects don''t have a read_until() method. You could say
something like:

f = open(filename)
lines = []
for line in f:
lines.append(line)
if ''</CsInstruments>'' in line:
break

4) filename[-3:] will give you the last 3 chars in filename. I''m
guessing that you want all but the last 3 chars, that''s filename[:-3],
but see the os.path.splitext() function, and indeed the other
functions in os.path too:
http://docs.python.org/lib/module-os.path.html

5) the regular expression objects returned by re.compile() will always
evaluate True, so you want to call their search() method on the data to
search:

if not pattern1.search(line):

But, 6) using re for a pattern as simple as "</" is way overkill. Just
use ''in'' or the find() method of strings:

if "</" not in line:

or:

pos = line.find("</")
if pos == -1:
print >>orcfilename, line
else:
print >>orcfilename, line[:pos]

7) the "print >file" usage requires a file (or file-like object,
anything with a write() method I think) not a string. You need to use
it like this:

orcfile = open(orcfilename, ''w'')
#...
print >orcfile, line

8) If you have a list of lines anyway, you can use the writelines()
method of files to write them in one go:

open(orcfilename, ''w'').writelines(lines)

of course stripping out your unwanted data from that last line using
find() as shown above.

I hope this helps.

Check out the docs on file objects:
http://docs.python.org/lib/bltin-file-objects.html, but like I said,
if you''re dealing with html or html-like files, be sure to check out
beautifulsoup. Also, there''s the elementtree package for parsing XML
that could help here too.

~Simon


这篇关于关于将项目从一个文件移到另一个文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆