how can i decrease the time execution of this code:
tab_voyelle is a table of 2669433 different words
for elem in tab_voyelle:
words_match = []
elem = elem.strip()
Tab_elem_out = CheckImpli(elem,tab_voyelle)
if len(Tab_elem_out) != 0:
MonFichINImpli = open('/home/user/Bureau/MesTravaux/MatchingCorpus/in_imply_result.txt','w')
for i in range(len(Tab_elem_out)):
MonFichINImpli.write(elem + ' ' + Tab_elem_out[i] + '\n')
MonFichINImpli.close()
subprocess.call(['java', '-jar', '/home/user/Bureau/MesTravaux/MatchingCorpus/dist/ImpliMorphVer4.jar','-i', 'in_imply_result.txt','-o','Result_Impli.txt'])
words_match = MatchingArabicWords('Result_Impli.txt','in_imply_result.txt')
if len(words_match) != 0 :
MonFichierResultat.write('<mot id="'+elem+'">'+'\n')
for valeur in words_match:
valeur = valeur.strip()
MonFichierResultat.write('<val>'+valeur+'</val>'+'\n')
t = tab_voyelle[tab_voyelle.index(valeur)]
del tab_voyelle[tab_voyelle.index(valeur)]
MonFichierResultat.write('</mot>' + '\n')
if len(words_match) == 0:
MonFichierResultat.write('<mot id="'+elem+'">'+'\n'+'<val></val>\n'+'</mot>' + '\n')
MonFichierResultat.write('</matching>' + '\n')
MonFichierResultat.close()
Related
I have a text file from which I need to extract some values, but several times I get more than one value between the beginning and the last line that starts with 0089 (CONVENIO) in the quadrant.
With my code I can only make it write in the txt the title I defined and it keeps repeating the first agreement found, but I need it to scroll through the text and bring me new information from the other quadrants.
I need the loop as it can have multiple covenants, for this reason I can't directly "anchor" the lines in a size forecast.
import re
import os
inicio = (' YM-INFRA-CASH MANAGMENT DEST.: 001-0001-CENTRAL ')
lista = []
contador = 3
banco = ' 0089'
convenio = ''
with open(caminho + '/Downloads/TESTE.txt', 'r') as arquivo:
for line in arquivo:
if line.strip() == inicio.strip():
localizar = arquivo.readlines()
inicio = localizar[contador]
van = inicio[13:17]
nomevan = inicio[20:50].strip()
inicio = localizar[contador + 1]
ag = inicio[13:17]
nomeag = inicio[20:50].strip()
inicio = localizar[contador + 2]
cliente = inicio[13:50].strip()
contadorum = 9
while localizar[contadorum][1:5] == '0033':
convenio = localizar[contadorum][1:22].strip()
narqrem = localizar[contadorum][22:34].strip()
bytesrem = localizar[contadorum][34:51].strip()
narqret = localizar[contadorum][51:63].strip()
bytesret = localizar[contadorum][63:81].strip()
totalbytes = localizar[contadorum][81:99].strip()
percrateio = localizar[contadorum][99:112].strip()
print(van, nomevan)
print(ag, nomeag)
print(cliente)
print(convenio, narqrem, bytesrem, narqret, bytesret, totalbytes, percrateio)
lista.append(convenio + narqrem + bytesrem + narqret + bytesret + totalbytes + percrateio +'\n')
with open(caminho + '/Downloads/testefim.txt', 'w') as consolidado:
consolidado.write('CONVENIO' + ';' + 'N ARQ REMES' + ';' + 'BYTES REMES' + ';' + 'N ARQ.RET.' + ';' + 'BYTES RET.' + ';' + 'TOTAL BYTES' + ';' + '% RATEIO' + '\n')
for linha in lista:
consolidado.write(convenio + ';' + narqrem + ';' + bytesrem + ';' + narqret + ';' + bytesret + ';' + totalbytes + ';' + percrateio + '\n')
consolidado.close()
else:
pass
arquivo.close()
I have a nested loop that works as it should on the first item in the list but then finishes
not sure why it doesn't move the second item?.
serials =['BHT350018400', 'BHTM0016380']
url_latency = 'https://my_company/ui/index.html#'
file_system_start_string = '/filesystems/unif/'
file_system_end_string = '__FILESYSTEM__fs_'
section = 'performance'
count = 1
file_system_count = (str(count))
for serial in serials:
for file in serial:
while count < 5:
full_url = url_latency + file_system_start_string + serial + file_system_end_string + file_system_count + '/' + section
driver.get(full_url)
driver.maximize_window()
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="shell-plugin-area"]/div/div/div/div[2]/ciq-tab-panel/div/div/div/div[3]/div/div/div/div/div/div/div[2]/ciq-performance-details/div/div[1]')))
screenShot_path = "\screengrab" + file_system_count + ".png"
screenshot = driver.save_screenshot(file_path + screenShot_path)
time.sleep(1)
count += 1
file_system_count = (str(count))
print(full_url)
You need to reset the 'count' variable
Like this, for example:
serials =['BHT350018400', 'BHTM0016380']
url_latency = 'https://my_company/ui/index.html#'
file_system_start_string = '/filesystems/unif/'
file_system_end_string = '__FILESYSTEM__fs_'
section = 'performance'
count = 1
file_system_count = (str(count))
for serial in serials:
for file in serial:
count=1 # <------------ here
while count < 5:
full_url = url_latency + file_system_start_string + serial + file_system_end_string + file_system_count + '/' + section
driver.get(full_url)
driver.maximize_window()
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="shell-plugin-area"]/div/div/div/div[2]/ciq-tab-panel/div/div/div/div[3]/div/div/div/div/div/div/div[2]/ciq-performance-details/div/div[1]')))
screenShot_path = "\screengrab" + file_system_count + ".png"
screenshot = driver.save_screenshot(file_path + screenShot_path)
time.sleep(1)
count += 1
file_system_count = (str(count))
print(full_url)
Or anywhere else, depends on program logic
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:] + ", ")
I have the following code which prints the data I have available but currently looks bad and cluttered:
has_printed = 0
output = ''
is_banner_printed = 0
if time.time() > next_print_time:
output = output + (' ' + datetime.now().strftime("%I:%M:%S %p") + ' ---------------------------------------------------------------------------------c')
is_banner_printed = 1
if is_banner_printed or not(len(self.post_list)):
print(output)
output = ''
for i, post in enumerate(self.post_list):
post.update()
output = output + (' Age: ' + datetime.fromtimestamp(self.utils.get_submission_age(post.created_utc)).strftime("%M:%S") +
' | URL: http://url.com/' + post.id +
' | Score: ' + str(post.score).ljust(6, ' ') + ... )
if i == 0:
output = '\n' + output
output = output + '\n'
if output is not '' and time.time() > next_print_time and is_banner_printed:
print(output)
has_printed = 1
next_print_time = time.time() + self.PRINT_RATE
if has_printed:
print(' ---------------------------------------------------------------------------------------------c\n\n\n')
elif is_banner_printed and output is '':
print('')
next_print_time = time.time() + self.PRINT_RATE
I wonder if it's possible to clean this mess to make the actual code readable?
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 !