This is my code to create a hashtag file. The issue is it does not put the # for the first hashtag and at he end it puts a double hashtag like below.
passiveincome, #onlinemarketing, #wahmlife, #cash, #entrepreneurlifestyle, #makemoneyonline, #makemoneyfast, #entrepreneurlifestyle, #mlm, #mlm
How do I get the code to remove the double output and put the # at the beginning?
import random, os, sys
basepath = os.path.dirname(sys.argv[0]) + "/"
outputpath = "C:/Users/matth/OneDrive/Desktop/Create hashtags/"
paragraphsmin = 9
paragraphsmax = 9
sentencemin = 1
sentencemax = 1
keywords = []
for line in open(basepath + "/base.txt", "r"):
keywords.append(line.replace("\n",""))
keywordlist = []
keyword = open(basepath + "/text-original.txt", "r")
for line in keyword:
keywordlist.append(line.replace("\n", "\n"))
def type(name):
value = name[random.randint(0,len(name)-1)]
return value
"""
def xyz(num):
s1 = '' + type(keywordlist).strip()
return eval('s' + str(num))
"""
def s1():
return '' + type(keywordlist).strip()
def randomSentence():
sent = eval("s" + str(random.randint(1,1)) + "()")
return sent
for keyword in keywords:
outputfile = open(outputpath + keyword.replace(" ", " ") + ".txt", "w")
outputfile.write('')
for p in range(1,random.randint(paragraphsmin,paragraphsmax) + 1):
outputfile.write('')
for s in range(1,random.randint(sentencemin,sentencemax) + 1):
sentence = randomSentence()
if str(sentence)[0] == "\"":
outputfile.write("" + str(sentence)[0] + str(sentence)[1] + str(sentence)[2:] + " ")
else:
outputfile.write("" + str(sentence)[0] + str(sentence)[1:] + ", #")
outputfile.write('')
outputfile.write(sentence.replace("", "") + "")
outputfile.close()
Try replacing
outputfile.write("" + str(sentence)[0] + str(sentence)[1:] + ", #")
with
outputfile.write("#" + str(sentence)[0] + str(sentence)[1:] + ", ")
wrote a simple program and would like to print the output to a text file. I'm trying the following at the very top of my script:
python SagicorPremiumCalculator2.py > textfile.txt
But this is giving me a syntax error in idle. How is my syntax incorrect?
python SagicorPremiumCalculator2.py > textfile.txt
python SagicorPremiumCalculator2.py > textfile.txt
import openpyxl, os, sys
wb = openpyxl.load_workbook('example.xlsx')
Millar_sheet = wb.get_sheet_by_name('Millar')
client = []
output = []
print(os.getcwd())
for rowOfCellObjects in Millar_sheet['A2':'AA13']:
for cellObj in rowOfCellObjects:
#if cellObj.value != None:
#print(cellObj.coordinate, cellObj.value)
client.append(cellObj.value)
#print(client[2]) #client name
print(client[0]) #policy number
print(client[9]) #license plate
#output.append(client[0]) #1
#output.append(client[9]) #2
if client[12] != "Not Applicable":
float(client[12])
print('S.I. = ' + '$' + str("%0.2f" % client[12]))
#output.append("%0.2f" % client[12]) #3
else:
print('S.I. ' + client[12])
print('Basic = ' + '$' + str("%0.2f" % client[13]))
#output.append("%0.2f" % client[13])#3
if client[14] != None:
print('Loading = ' + str(100*client[14]) + '%')
else:
print('No Loading')
val = client[13]
if client[14] == None:
val = client[15] #if there is no loading percentage, val becomes subtotal
else:
val += (val*client[14]) # a = a + b, adds 150 to 1000
print('Subtotal = ' + '$' + str("%0.2f" % val))#good
if client[16] != None:
ATD = val*client[16] #discount amount
print('ATD = ' + str(100*client[16]) + '%, -$' + str("%0.2f" % ATD))
val -= (val*client[16])
print('$' + str("%0.2f" % val))
if client[17] != None:
val -= (val*client[17])
SP = val*client[17] #Discount amount
print('SP = ' + str(100*client[17]) + '%, -$' + str("%0.2f" % SP))
print('$' + str("%0.2f" % val))
if client[18] != None:
val = (val+client[18])
PAB = client[18]
print('PAB = +$' + str(client[18]))
print('$' + str("%0.2f" % val))
if client[19] != None:
NCD = val*client[19]
val -= (val*client[19])#discount amount
print('NCD = ' + str(100*client[19]) +'%, -$' + str("%0.2f" % NCD))
print('$' + "%0.2f" % val)
if client[20] != None:
val = (val+client[20])
print('W/S = +$' + str(client[20]))
print('$' + "%0.2f" % val)
if client[21] != None:
val = (val+client[21])
print('LOU = $' + str(client[21]))
print('$' + "%0.2f" % val)
if val < 750: #Applies minimum premium if val is under 750
print("Minimum premium applied")
val = 750
print('Pre-tax Premium is ' + '$' + str("%0.2f" % val)) #good
if client[23] == 0: #Client over 65, not being charged tax
print('According to Sagicor speadsheet, client over 65')
else:
PreTax = val*0.06
val = (PreTax+val)
print('Premium Tax = +$' + str("%0.2f" % PreTax)) #good
print('$' + "%0.2f" % val)
if client[25] != None:
print('RS = +$' + str(client[25]))
val = (val+client[25])
print('Total = $' + str("%0.2f" % val))
#if val == client[26]:
#print('Sagicor\'s total agrees with calculated total - top')
#else:
#print('Premium does not agree - top')
else:
print('Roadside assistance NOT included')
print('Total = $' + str("%0.2f" % val))
#if val == client[26]:
#print('Sagicor\'s total agrees with calculated total - bottom')
#else:
#print('Premium does not agree - bottom')
client = []
output = []
print('--- END OF ROW ---')
If you want to write the output of your script in a file I see two options.
The first one is to handle the writing in the python script :
#!/usr/bin/python
#I am here assuming that you use a Unix-like system (i.e. Not Windows )
file = open('textfile.txt','w')
... some code ...
file.write(myData +'\n')
#Don't forget to close the file at the end of the script
file.close()
N.B: the 'w' option creates the file if it not exists, but it aslo delete previous version of the file. If you want to add lines at the end of an existing file, you should use 'a' instead.
The '\n' is just here to create a new line.
The other option is the following : Simple use print statament in your script
#My Script
... some code ...
print MyData
and then, run your script in a shell not in IDLE with the following command :
python SagicorPremiumCalculator2.py > textfile.txt
If you are running Windows, you should definitely use the first option (because I am not sure the bash-style commands works in cmd ).
I hope this helps !
I have the following code where I construct HTML rows,now when key is present in dict1 I want to color the row and when key is present in dict2 another color?
how can I modify this code to color the rows?
for item in jiradb :
MailBody = MailBody + "<tr>"
MailBody = MailBody + "<td>" + str(icount) + "</td>"
print get_field(item, 'key')
key = item['key']
key_after_none_check = get_field(item,'key');
crashid_link = "https://company.com/data/browse/" + key;
key_present_in_anr_tombstone = False;
if ((key in dict1) and (dict1[key] !=0)):
key_present_in_anr_tombstone = True
#MailBody "" + str(get_field(item,'key ')) + ";
MailBody = MailBody + "<td>" + "" + str(key_after_none_check) + "" + "(" + "x" + str(dict1[key]) + ")" + "</td>"
if ((key in dict2) and (dict2[key] !=0)):
key_present_in_anr_tombstone = True
MailBody = MailBody + "<td>" + "" + str(key_after_none_check) + "" + "(" + "x" + str(dict2[key]) + ")" + "</td>"
if key_present_in_anr_tombstone == False:
MailBody = MailBody + "<td>" + "" + str(key_after_none_check) + "" + "</td>"
MailBody = MailBody + "<td style=\"width:100%\">" + get_field(item,'summary') + "</td>"
MailBody = MailBody + "<td style=\"width:100%\">" + get_field(item,'Resolution') + "</td>"
icount = icount + 1
MailBody = MailBody + "</tr>"
To solve your problem, try to fetch the values first, and use css styling to color the rows.
rows = []
for item in jiradb:
data = []
key = item['key']
dict1_value = dict1.get(key)
dict2_value = dict2.get(key)
key_after_none_check = get_field(item,'key')
crashid_link = "https://company.com/data/browse/{}".format(key)
row = '<td><span style="background-color:{};">{}(x{})</span></td>'
color = 'white' # default color
if dict1_value != None:
color = 'red';
data.append(row.format(color, crashid_link, key_after_none_check, dict1_value))
if dict2_value != None:
color = 'blue'
data.append(row.format(color, crashid_link, key_after_none_check, dict2_value))
if not dict1_value and not dict2_value:
data.append('<td>{}</td>'.format(crashid_link, key_after_none_check))
data.append('<td style="width:100%">{}</td>'.format(get_field(item, 'summary')))
data.append('<td style="width:100%">{}</td>'.format(get_field(item, 'Resolution')))
rows.append(data)
mail_rows = []
for count,row in enumerate(rows):
head = '<tr><td>{}</td></tr>'.format(count+1)
body = '\n'.join(row)
footer = '</tr>'
mail_rows.append('{}{}{}'.format(head,body,footer))
mail_body = ''.join(mail_rows)
CURRENT OUTPUT:-
Your code is quite a tangled mess. I'm not sure if I got it all right, but take a look at this version. I don't know where icount comes from, but you can get it from enumerate. I made a string to contain what I think is a template for your table rows. Then I took out all the repetitive logic in the if clauses. Collected the values, and then popped them into the template.
for icount, item in enumerate(jiradb) :
vars = []
MailBody = MailBody +
"""<tr><td>{0}</td>
<td>{2}(x{3})</td>
<td style='width:100%'>{4}</td>
<td style='width:100%'>{5}</td></tr>
"""
vars.append(icount+1)
key = item['key']
vars.append(key)
key_after_none_check = get_field(item,'key');
vars.append(key_after_none_check)
key_present_in_anr_tombstone = False;
if ((key in dict1) and (dict1[key] !=0)):
key_present_in_anr_tombstone = True
vars.append(dict1[key])
if ((key in dict2) and (dict2[key] !=0)):
key_present_in_anr_tombstone = True
vars.append(dict2[key])
if key_present_in_anr_tombstone == False:
vars.append(key_after_non_check)
vars.append(get_field(item, 'summary'))
vars.append(get_field(item, 'Resolution'))
MailBody.format(*vars)
Environment
I am using Python 3 and my OS is Windows 7.
I understand that some commands have changed from the transition from python 2.7 to 3 (What I have used).
Problem
The variable is temporary but is this:
(((((0, 7), 7), 8), 4), 5)
Here is the code to get rid of the brackets:
randy = randy.replace(")", "")
randy = randy.replace("(", "")
randy = randy.replace(" ", "")
When it tries to execute the replace function, I get thrown an error:
Traceback (most recent call last):
File "<string>", line 248, in run_nodebug
File "Criptic.py", line 134, in <module>
randy = randy.replace(")", "")
AttributeError: 'tuple' object has no attribute 'replace'
Edit:
Here is all of the code:
#Import string
import string
import random
#Input user data
text = input("Enter your text to be converted: ")
#Test Print
print("-------------------------")
print("Your text is: ",text)
#Break up all the data
data = list(text)
#Lowercase
count = text.count("a")
count1 = text.count("b")
count2 = text.count("c")
count3 = text.count("d")
count4 = text.count("e")
count5 = text.count("f")
count6 = text.count("g")
count7 = text.count("h")
count8 = text.count("i")
count9 = text.count("j")
count10 = text.count("k")
count11 = text.count("l")
count12 = text.count("m")
count13 = text.count("n")
count14 = text.count("o")
count15 = text.count("p")
count16 = text.count("q")
count17 = text.count("r")
count18 = text.count("s")
count19 = text.count("t")
count20 = text.count("u")
count21 = text.count("v")
count22 = text.count("w")
count23 = text.count("x")
count24 = text.count("y")
count25 = text.count("z")
count26 = text.count("A")
#Uppercase
count27 = text.count("B")
count28 = text.count("C")
count29 = text.count("D")
count30 = text.count("E")
count31 = text.count("F")
count32 = text.count("G")
count33 = text.count("H")
count34 = text.count("I")
count35 = text.count("J")
count36 = text.count("K")
count37 = text.count("L")
count38 = text.count("M")
count39 = text.count("N")
count40 = text.count("O")
count41 = text.count("P")
count42 = text.count("Q")
count43 = text.count("R")
count44 = text.count("S")
count45 = text.count("T")
count46 = text.count("U")
count47 = text.count("V")
count48 = text.count("W")
count49 = text.count("X")
count50 = text.count("Y")
count51 = text.count("Z")
#Other Characters
count52 = text.count(" ")
count53 = text.count("?")
count54 = text.count("#")
count55 = text.count("(")
count56 = text.count(")")
count57 = text.count(".")
#Numbers
count58 = text.count("1")
count59 = text.count("2")
count60 = text.count("3")
count61 = text.count("4")
count62 = text.count("5")
count63 = text.count("6")
count64 = text.count("7")
count65 = text.count("8")
count66 = text.count("9")
count67 = text.count("0")
#Counting how many characters in the sentence
finalcount = count + count1 + count2 + count3 + count4 + count5 + count6 + count7 + count8 + count9 + count10 + count11 + count12 + count13 + count14 + count15 + count16 + count17 + count18 + count19 + count20 + count21 + count22 + count23 + count24 + count25 + count26 + count27 + count28 + count29 + count31 + count32 + count33 + count34 + count35 + count36 + count37 + count38 + count39 + count40 + count41 + count42 + count43 + count44 + count45 + count46 + count47 + count48 + count49 + count50 + count51 + count52 + count53 + count54 + count55 + count56 + count57 + count58 + count59 + count60 + count61 + count62 + count63 + count64 + count65 + count66 + count67
#Final count of Characters
print(" Chars: ",finalcount)
print("-------------------------")
char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!##$%^&*()?_+-=1234567890 "
charnum = 74 + 11
list(char)
randy = 0
num = 0
while num < finalcount:
rand = random.randrange(1,9)
randy = randy,rand
finalcount = finalcount - 1
if rand == 1:
print(text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 2:
print(char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 3:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 4:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 5:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 6:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 7:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],",")
if rand == 8:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],char[random.randrange(0,charnum)],",")
if rand == 9:
print(char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],char[random.randrange(0,charnum)],text[finalcount],",")
randy = randy.replace(")", "")
randy = randy.replace("(", "")
randy = randy.replace(" ", "")
print("-------------------------")
print("::::::::Completed::::::::")
print("-------------------------")
print(randy)
print("-This is your code unlock key. Without this code it will not decript!!!! ")
print("-------------------------")
You have a tuple (a sort of list) of numbers, and you want to make that a string. You can't replace the brackets, they aren't a part of the variable, they are just a part of its representation.
Instead you should use join() and str()
result = " ".join(str(x) for x in randy)
However, this probably will not give the right result either, as it is a nested list of tuples. You probably mean: randy = randy + (rand,) instead if randy = randy,rand.
randy = randy,rand
This notation creates a tuple of two elements, tuples do not have a method called replace.
If you just want to concatenate the numbers into a list representation separated by commas, you could do this in your loop:
randy = []
num = 0
while num < finalcount:
rand = random.randrange(1,9)
randy.append(rand)
Then you remove the replace lines, and instead do this
randy = ",".join(randy)
to get a string that has the values separated by commas.
You should maybe add the output that you would like to get to your question, because at the moment it's not quite clear what you want to do exactly.
exam_st_date = (11, 12, 2014)
# Sample Output : The examination will start from : 11 / 12 / 2014
newtup = str(exam_st_date).replace(',', ' /')
print(newtup)
look at mine example should give you some results
To delete a list of characters, you could use the string's translate method:
import string
name = "(((((0, 7), 7), 8), 4), 5)"
table = string.maketrans( '', '', )
print name.translate(table,"()")