寻求帮助 - 字符串处理。 [英] Seeking assistance - string processing.

查看:58
本文介绍了寻求帮助 - 字符串处理。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一些代码来搜索特定的文本字符串,并且

以某种方式对他们采取行动。我已经对转换进行了排序然而

仍然存在1个问题。


我正在尝试找出如何让它找到这样的字符串" ==="

,当它找到它时,我希望它添加===到

行的末尾。


例如。


文本文件包含:


===标题


我试图将其作为.dat文件进行处理和输出

内容


===标题===


这是我到目前为止的代码。


导入字符串

导入glob

导入os


mydir = os.getcwd()

newdir = mydir#+" \\Test \\" ;;

表示glob.glob1中的文件名(newdir," * .txt"):

#print"这是一个输入文件:" + filename

fileloc = newdir +" \\" + filename

#print fileloc

outputname = filename

outputfile = string.replace(outputname,''。txt'',''。dat'')

#print filename

#打印一个


print"这是一个输入文件:" + filename +"。输出文件:

" + outputfile


#temp = newdir +" \\" + outputfile

#print temp

fpi = open(fileloc);

fpo = open(outputfile," w +");


output_lines = []

lines = fpi.readlines()


for line in lines:

如果line.rfind(" --------------------")不是-1:

new = line.replace(" --------------------"," ----")

elif line。 rfind(" img:")不是-1:

new = line.replace(" img:"," [[Image:")

elif line.rfind(" .jpg")不是-1:

new = line.replace(" .jpg"," .jpg]]")

elif line.rfind(" .gif")不是-1:

new = line.replace(" .gif"," .gif]]")

else:

output_lines.append(line);

继续

output_lines.append(new); <对于output_lines中的行,


fpo.write(行)


fpi.close()

fpo.flush()

fpo.c丢失()

我希望这个格式正确:-p


干杯,希望你能帮忙。

I''ve been working on some code to search for specific textstrings and
act upon them insome way. I''ve got the conversion sorted however there
is 1 problem remaining.

I am trying to work out how to make it find a string like this "==="
and when it has found it, I want it to add "===" to the end of the
line.

For example.

The text file contains this:

===Heading

and I am trying to make it be processed and outputted as a .dat file
with the contents

===Heading===

Here''s the code I have got so far.

import string
import glob
import os

mydir = os.getcwd()
newdir = mydir#+"\\Test\\";

for filename in glob.glob1(newdir,"*.txt"):
#print "This is an input file: " + filename
fileloc = newdir+"\\"+filename
#print fileloc

outputname = filename
outputfile = string.replace(outputname,''.txt'',''.dat'')
#print filename
#print a

print "This is an input file: " + filename + ". Output file:
"+outputfile

#temp = newdir + "\\" + outputfile
#print temp
fpi = open(fileloc);
fpo = open(outputfile,"w+");

output_lines = []
lines = fpi.readlines()

for line in lines:
if line.rfind("--------------------") is not -1:
new = line.replace("--------------------","----")
elif line.rfind("img:") is not -1:
new = line.replace("img:","[[Image:")
elif line.rfind(".jpg") is not -1:
new = line.replace(".jpg",".jpg]]")
elif line.rfind(".gif") is not -1:
new = line.replace(".gif",".gif]]")
else:
output_lines.append(line);
continue
output_lines.append(new);

for line in output_lines:
fpo.write(line)

fpi.close()
fpo.flush()
fpo.close()
I hope this gets formatted correctly :-p

Cheers, hope you can help.

推荐答案

bi ******* *******@googlemail.com 写道:
bi**************@googlemail.com wrote:

我正在尝试找出如何让它找到像这样的字符串 ==="

当它找到它时,我希望它添加===到

行的末尾。
I am trying to work out how to make it find a string like this "==="
and when it has found it, I want it to add "===" to the end of the
line.



怎么样


如果line.startswith(" ==="):

line = line +" ==="





if" ==="排队:#任何地方

line = line +" ==="



how about

if line.startswith("==="):
line = line + "==="

or

if "===" in line: # anywhere
line = line + "==="

?


如果line.rfind(" --------------------")不是-1:

new = line。 replace(" --------------------"," ----")
if line.rfind("--------------------") is not -1:
new = line.replace("--------------------","----")



对于不包含

模式的字符串使用replace并不是错误,所以rfind是不必要的。


(对于需要先查看的情况,从左侧搜索

通常比向后搜索更快;使用模式在线或

行.find(pattern)"而不是rfind。


< / F>

it''s not an error to use replace on a string that doesn''t contain the
pattern, so that rfind is rather unnecessary.

(and for cases where you need to look first, searching from the left
is usually faster than searching backwards; use "pattern in line" or
"line.find(pattern)" instead of rfind.

</F>


非常感谢确实是一个非常优雅的解决方案。


我还有另外一个问题,我正在祈祷你可以帮助我

with:


关于.jpg转换为.jpg]和.gif -.gif ]]


这是有效的,但只有当.jpg / .gif在它自己的行上时才有效。


ie:


..jpg


将转换为:


..jpg]]


但是


图片:test.jpg


转换为:

[[Image:test.jpg

而不是


[[Image:test.jpg]]


------------------


希望你能再次帮助!干杯

Thanks so much, a really elegant solution indeed.

I have another question actually which I''m praying you can help me
with:

with regards to the .jpg conversion to .jpg]] and .gif -.gif]]

this works, but only when .jpg/.gif is on it''s own line.

i.e:

..jpg

will get converted to:

..jpg]]

but

Image:test.jpg

gets converted to:

[[Image:test.jpg

rather than

[[Image:test.jpg]]

------------------

Hope you can help again! Cheers


bi * *************@googlemail.com 写道:
bi**************@googlemail.com wrote:

非常感谢,这确实是一个非常优雅的解决方案。


我还有另一个问题,我正在祈祷你可以帮助我

with:


如有问候到.jpg转换为.jpg]]和.gif -.gif]]


这是有效的,但只有当.jpg / .gif在它自己的行上时才有效。


ie:


.jpg


将转换为:


.jpg]]


但是


图片:test.jpg


转换为:


[[Image:test.jpg


而不是


[[Image:test.jpg]]


------------------


希望你能再次帮助!干杯
Thanks so much, a really elegant solution indeed.

I have another question actually which I''m praying you can help me
with:

with regards to the .jpg conversion to .jpg]] and .gif -.gif]]

this works, but only when .jpg/.gif is on it''s own line.

i.e:

.jpg

will get converted to:

.jpg]]

but

Image:test.jpg

gets converted to:

[[Image:test.jpg

rather than

[[Image:test.jpg]]

------------------

Hope you can help again! Cheers



在所有情况下它都没有做正确的事情,但也许你可以通过线路输入


line:

if line.startswith(" ==="):

line = line.rstrip()+" === \ n"

line = line.replace(" --------------------"," ----")

line = line.replace(" img:"," [[Image:")

line = line.replace(" .jpg","。 jpg]]")

line = line.replace(" .gif"," .gif]]")

output_lines.append(line)


彼得

It does not do the right thing in all cases, but maybe you can get away with

for line in lines:
if line.startswith("==="):
line = line.rstrip() + "===\n"
line = line.replace("--------------------","----")
line = line.replace("img:","[[Image:")
line = line.replace(".jpg",".jpg]]")
line = line.replace(".gif",".gif]]")
output_lines.append(line)

Peter


这篇关于寻求帮助 - 字符串处理。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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