0

I have a file like this

A0001
/1536.
/1537.
/1511.
/1451.
/1388.
/1323.
/1322.


# And so on...

I used json .format to make it like this

<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 

But that's not the output I was looking for, what I'm looking for is this:

<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>

I want every string to be on one line. as I'm new to python and I don't know what to search for to get an answer to my question, This is my code

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()


TheNerdy97
  • 27
  • 5

2 Answers2

0

It seems like you are missing an action to strip the new line.

You could add a replace after the first replace like this

def remove_punc(string):
    punc = '''!()-[]{};:'"\, <>./?@#$%^&*_~'''
    for ele in string:  
        if ele in punc:  
            string = string.replace(ele, "")**.replace('\n','')**
    return string

Maybe take a look at this question.

Pascal
  • 342
  • 1
  • 7
0

You have to remove the newline character ('\n') from the end of each line. This can be done with strip.

I rewrote the remove_punc function to use translate to make it faster.

This replaces your full code example.

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')
Matthias
  • 12,873
  • 6
  • 42
  • 48
  • you went beyond what I thought of! Thanks this is a fantastic answer! – TheNerdy97 Jun 01 '22 at 09:36
  • Some short explanations: I used the `with` statement to automatically close the files. I dropped `readlines` because you can iterate over the lines of a text file. I used an f-string instead of format. I added a second parameter with a default value to `remove_punc` so that you can provide the function with a different set of characters. And `translate` ... well, I knew that it existed and had to look it up in the documentation. :) – Matthias Jun 01 '22 at 09:41