Edit: I put the results i wanted, it seems like my question was unclear, sorry!
I need a help to delete my place holder, here is more details of what i meant with my question:
Say this is my dataframe:
import pandas as pd
data = {'product_name': ['laptop', 'printer'],
'price': [1200, 150]
}
df = pd.DataFrame(data)
I created a .txt file as a medium to replace the place holder with the dataframe content. The .txt file looks like this:
placeholder0!
placeholder1!
placeholder2!
placeholder3!
I want to use panda to open the .txt file, replace the place holder with some value from the dataframe, and DELETE the rest of the place holder. Currently my code looks like below.
runner = open(('runner.run'),'w')
note = open(('TEMPLATE.TXT'),'r').read()
x = 0
y = len(df.index)
for x in df.index:
if (df.product_name.iloc[x] == 'laptop') :
note = note.replace('placeholder'+str(x)+'!' , 'laptop')
x +=1
else :
note = note.replace('placeholder'+str(x)+'!', 'xxxx')
x +=1
#to clean up the rest of the place holder
for x in df.index:
note = note.replace('placeholder'+str(y)+'!', '')
y+=1
output = open('RESULT.TXT', 'w')
output.write(note)
output.close()
runner.close()
Result:
laptop
xxxxx
(empty string)
(empty string)
The result i want in the .TXT:
laptop
xxxxx
The result i got the the RESULT.TXT is the place holder replaced by empty string (as written in the code because i don't know how to delete them). What i really want is to have all the placeholder deleted. So the .TXT file will have 2 rows in total instead of 4 rows (2 rows for results and 2 rows for blanks) Could you please help me?
I hope i am clear in explaning this. Thank you in advance!
Kind regards.