文件中的字符串在一行
string in file on one line
我有一个这样的文件
A0001
/1536.
/1537.
/1511.
/1451.
/1388.
/1323.
/1322.
# And so on...
我用 json .format 做成这样
<div class="parent"><img class="img" title="" src="resources/images/thumb/A0001
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1536
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1537
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1511
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1451
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1388
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1323
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1322
.png"width="64" height="64"></div>
#And so on
但这不是我要找的输出,我要找的是这个:
<div class="parent"><img class="img" title=""src="resources/images/thumb/1242.png"width="64" height="64"></div>
<div class="parent"><img class="img" title=""src="resources/images/thumb/1536.png"width="64" height="64"></div>
<div class="parent"><img class="img" title=""src="resources/images/thumb/1242.png"width="64" height="64"></div>
我希望每个字符串都在一行上。由于我是 python 的新手,我不知道要搜索什么才能得到问题的答案,这是我的代码
import re
import pyperclip
import json
def remove_punc(string):
punc = '''!()-[]{};:'"\, <>./?@#$%^&*_~'''
for ele in string:
if ele in punc:
string = string.replace(ele, "")
return string
d = '<div class="parent"><img class="img" title="" src="resources/images/thumb/{}.png"></div>'
rex = re.compile(p)
f= open('txte.txt', 'r')
lines = f.readlines()
results = [remove_punc(i) for i in lines]
f.close()
fp= open("data1.txt", "w")
for item in results:
print(d.format(item), file=fp)
fp.close()
您似乎缺少删除新行的操作。
您可以像这样在第一个替换之后添加一个替换
def remove_punc(string):
punc = '''!()-[]{};:'"\, <>./?@#$%^&*_~'''
for ele in string:
if ele in punc:
string = string.replace(ele, "")**.replace('\n','')**
return string
也许看看 this question。
您必须删除每行末尾的换行符 ('\n'
)。这可以通过 strip
.
来完成
我重写了 remove_punc
函数以使用 translate
使其更快。
这将替换您的完整代码示例。
def remove_punc(text, punc='''!()-[]{};:'"\, <>./?@#$%^&*_~'''):
return text.translate(str.maketrans('', '', punc)).strip()
with open('txte.txt') as names, open("data1.txt", "w") as target:
for name in names:
target.write(f'<div class="parent"><img class="img" title="" src="resources/images/thumb/{remove_punc(name)}.png"></div>\n')
我有一个这样的文件
A0001
/1536.
/1537.
/1511.
/1451.
/1388.
/1323.
/1322.
# And so on...
我用 json .format 做成这样
<div class="parent"><img class="img" title="" src="resources/images/thumb/A0001
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1536
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1537
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1511
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1451
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1388
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1323
.png"width="64" height="64"></div>
<div class="parent"><img class="img" title="" src="resources/images/thumb/1322
.png"width="64" height="64"></div>
#And so on
但这不是我要找的输出,我要找的是这个:
<div class="parent"><img class="img" title=""src="resources/images/thumb/1242.png"width="64" height="64"></div>
<div class="parent"><img class="img" title=""src="resources/images/thumb/1536.png"width="64" height="64"></div>
<div class="parent"><img class="img" title=""src="resources/images/thumb/1242.png"width="64" height="64"></div>
我希望每个字符串都在一行上。由于我是 python 的新手,我不知道要搜索什么才能得到问题的答案,这是我的代码
import re
import pyperclip
import json
def remove_punc(string):
punc = '''!()-[]{};:'"\, <>./?@#$%^&*_~'''
for ele in string:
if ele in punc:
string = string.replace(ele, "")
return string
d = '<div class="parent"><img class="img" title="" src="resources/images/thumb/{}.png"></div>'
rex = re.compile(p)
f= open('txte.txt', 'r')
lines = f.readlines()
results = [remove_punc(i) for i in lines]
f.close()
fp= open("data1.txt", "w")
for item in results:
print(d.format(item), file=fp)
fp.close()
您似乎缺少删除新行的操作。
您可以像这样在第一个替换之后添加一个替换
def remove_punc(string):
punc = '''!()-[]{};:'"\, <>./?@#$%^&*_~'''
for ele in string:
if ele in punc:
string = string.replace(ele, "")**.replace('\n','')**
return string
也许看看 this question。
您必须删除每行末尾的换行符 ('\n'
)。这可以通过 strip
.
我重写了 remove_punc
函数以使用 translate
使其更快。
这将替换您的完整代码示例。
def remove_punc(text, punc='''!()-[]{};:'"\, <>./?@#$%^&*_~'''):
return text.translate(str.maketrans('', '', punc)).strip()
with open('txte.txt') as names, open("data1.txt", "w") as target:
for name in names:
target.write(f'<div class="parent"><img class="img" title="" src="resources/images/thumb/{remove_punc(name)}.png"></div>\n')