Update a HTML table in Python - python

I am a novice coder. I have a HTML table that looks like this and is dynamically created by a python script.
A B C D E F
G 1 1 0 0 1 1
H 1 0 1 2 4 5
Here is the code that generates this table:
f.write("<center><table align = 'center' border=\"1\" width=\"800\" height=\"40\"><tr><td align = 'center'><h3><b>Compared TaskLogs</h3></td><td align = 'center'><h3><b>Total</h3></td><td align = 'center'><h3><b>Passed</h3></td><td colspan = '3' align = 'center'><h3><b>Bug</h3></td><td align = 'center' colspan ='3'><h3><b>Script</h3></td><td align = 'center' colspan = '3'><h3><b>Setup</h3></td></tr></b>");
f.write("<tr><td></td><td></td><td></td>")
f.write("<td align = 'center'><h5><b>Known</b></h4></td><td align = 'center'><h5><b>Unknown</b></h4></td><td align = 'center'><h5><b>Total</b></h5>")
f.write("<td align = 'center'><h5><b>Known</b></h4></td><td align = 'center'><h5><b>Unknown</b></h4></td><td align = 'center'><h5><b>Total</b></h5>")
f.write("<td align = 'center'><h5><b>Known</b></h4></td><td align = 'center'><h5><b>Unknown</b></h4></td><td align = 'center'><h5><b>Total</b></h5>")
f.write("</tr>")
if 'text' in s:
counter_pass=0;
counter_kbug=0;
counter_kscr=0;
counter_kset=0;
counter_uscr=0;
counter_ubug=0;
counter_uset=0;
totbug = 0;
totscr = 0;
totset = 0;
tottc=0;
for key, value in tlogs.items():
print"entering loop: for key, value in tlogs.items(): VALUE:",value
print "ITEM:",s
if (value==s):
value1=tcstatus[key]
print "VALUE--", value1
if value1=="p":
counter_pass=counter_pass+1;
if value1=="kbug":
counter_kbug=counter_kbug+1;
if value1=="kscr":
counter_kscr=counter_kscr+1;
if value1=="kset":
counter_kset=counter_kset+1;
if value1=="ubug":
counter_ubug=counter_ubug+1;
if value1=="uscr":
counter_uscr=counter_uscr+1;
if value1=="uset":
counter_uset=counter_uset+1;
totbug = counter_kbug+counter_ubug
totscr = counter_kscr+counter_uscr
totset = counter_kset+counter_uset
tot = counter_pass+totbug+totscr+totset
f.write("<tr><td height=\"30\">"+str(tot)+"</td><td height=\"30\">"+str(counter_pass)+"</td><td height=\"30\">"+str(counter_kbug)+"</td><td>"+str(counter_ubug)+"</td><td height=\"30\">"+str(totbug)+"</td><td height=\"30\">"+str(counter_kscr)+"</td><td height=\"30\">"+str(counter_uscr)+"</td><td height=\"30\">"+str(totscr)+"</td><td height=\"30\">"+str(counter_kset)+"</td><td height=\"30\">"+str(counter_uset)+"</td><td height=\"30\">"+str(totset)+"</td></tr></center><br>");
f.write("</table></center></body>")
I have another text file that looks like this:
G,A
H,E
Now I need to update the table entry in the Gth row and Ath column by 1 and Hth row and Eth column by 1.
All this is done in python. Could someone help me please!

Related

Someone can help me check do i convert correctly the Chandelier Exit indicator from pine script code to python?

These pine script code is from trading view and write by everget. This indicator is Chandelier Exit. I am doing back test by using this indicator so I need convert this pine script code to python in order to get the buy and sell signal.
I am using GBPJPY data from 31/12/2019 to 1/1/2022, 4hrs charts. But the buysignal and sellsignal total just have 6 signal, i feel like something wrong with the result. Therefore, I am thinking do I convert correctly the code from pine script to python?
Python code :
# ATR calculation
atr_period = 2
h, l, c_prev,c = df['high'], df['low'], df['close'].shift(1),df['close']
tr = np.max([h - l, (c_prev - h).abs(), (c_prev - l).abs()], axis=0)
df['atr'] = pd.Series(tr).rolling(2).mean().bfill().values
# CE calculation
df['c1'] = pd.Series(h).rolling(2).max().bfill().values # Get the max value within two row
df['c2'] = pd.Series(l).rolling(2).min().bfill().values # Get the min value within two row
df['cel'] = df['c1'] - df['atr']*4
df['ces'] = df['c2'] + df['atr']*4
sh = df['ces'].to_numpy(dtype='float') # Convert to array
lo = df['cel'].to_numpy(dtype='float')
sh_prev = (df['ces'].shift(1)).to_numpy(dtype='float') # Shift downward to get the previous data in ces,cel and close
lo_prev = (df['cel'].shift(1)).to_numpy(dtype='float')
c_prev = (df['close'].shift(1)).to_numpy(dtype='float')
shnew = np.empty(3126, dtype=float) # Create new array to save new data
lonew = np.empty(3126, dtype=float)
for i in range(len(df['close'])): # Compare the previous close to previous cel
if c_prev[i] > lo_prev[i]:
lonew[i] = max(lo[i], lo_prev[i])
else:
lonew[i] = lo[i]
for i in range(len(df['close'])):# Compare the previous close to previous ces
if c_prev[i] < sh_prev[i]:
shnew[i] = min(sh[i], sh_prev[i])
else:
shnew[i] = sh[i]
# Create array for direction
# Follow the CE indicator to identify the direction
direc = np.empty(3126, dtype=int)
direc[3125] = 1 # Put a initial data into the last array
for i in range(len(df['close'])): # Compare the close to CE to get the direction
if c[i] > shnew[i]:
direc[i] = 1
elif c[i] < lonew[i]:
direc[i] = -1
else:
direc[i] = direc[i-1]
dfdir = pd.DataFrame(direc,columns=['direc'],index=df.index) # Convert the direction to dataframe
df = pd.merge(df, dfdir,left_index=True,right_index=True) # Merge the direction into ori dataframe
df['direc_prev'] = df['direc'].shift(1) # Shift downward to get the previous data
df['buysignal'] = np.logical_and(df['direc'] == 1,df['direc_prev'] == -1) # Get the buy signal (long)
df['sellsignal'] = np.logical_and(df['direc'] == -1,df['direc_prev'] == 1) # Get the sell signal (short)
Pine script code in trading view:
//#version=4
// Copyright (c) 2019-present, Alex Orekhov (everget)
// Chandelier Exit script may be freely distributed under the terms of the GPL-3.0 license.
study("Chandelier Exit", shorttitle="CE", overlay=true)
length = input(title="ATR Period", type=input.integer, defval=22)
mult = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
showLabels = input(title="Show Buy/Sell Labels ?", type=input.bool, defval=true)
useClose = input(title="Use Close Price for Extremums ?", type=input.bool, defval=true)
highlightState = input(title="Highlight State ?", type=input.bool, defval=true)
atr = mult * atr(length)
longStop = (useClose ? highest(close, length) : highest(length)) - atr
// if useClose = false
// longstop = highest(length) - atr (want this)
// else
// longstop = highest(close, length)
longStopPrev = nz(longStop[1], longStop)
// nz(x,y) = if previous value is valid then take x else y
longStop := close[1] > longStopPrev ? max(longStop, longStopPrev) : longStop
// if close[1] > longStopPrev
// longstop = max(longStop, longStopPrev)
// else
// longstop = longstop
shortStop = (useClose ? lowest(close, length) : lowest(length)) + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := close[1] < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop
var int dir = 1
dir := close > shortStopPrev ? 1 : close < longStopPrev ? -1 : dir
// if close > shortStopPrev
// dir = 1
// elseif close < longStopPrev
// dir = -1
//else
// dir = dir[-1]
var color longColor = color.green
var color shortColor = color.red
buySignal = dir == 1 and dir[1] == -1
// and = 1 & 1 > 1
plotshape(buySignal and showLabels ? longStop : na, title="Buy Label", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor, textcolor=color.white, transp=0)
sellSignal = dir == -1 and dir[1] == 1
plotshape(sellSignal and showLabels ? shortStop : na, title="Sell Label", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor, textcolor=color.white, transp=0)

Save geocoding results from address to longitude and latitude to original dataframe in Python

Given a small dataset df as follows:
id name address
0 1 ABC tower 北京市朝阳区
1 2 AC park 北京市海淀区
2 3 ZR hospital 上海市黄浦区
3 4 Fengtai library NaN
4 5 Square Point 上海市虹口区
I would like to obtain longitude and latidude for address column and append them to orginal dataframe. Please note there are NaNs in address column.
The code below gives me a table with addresses, longitude and latitude, but it ignores the NaN address rows, also the code should be improved:
import pandas as pd
import requests
import json
df = df[df['address'].notna()]
res = []
for addre in df['address']:
url = "http://restapi.amap.com/v3/geocode/geo?key=f057101329c0200f170be166d9b023a1&address=" + addre
dat = {
'count': "1",
}
r = requests.post(url, data = json.dumps(dat))
s = r.json()
infos = s['geocodes']
for j in range(0, 10000):
# print(j)
try:
more_infos = infos[j]
# print(more_infos)
except:
continue
try:
data = more_infos['location']
# print(data)
except:
continue
try:
lon_lat = data.split(',')
lon = float(lon_lat[0])
lat = float(lon_lat[1])
except:
continue
res.append([addre, lon, lat])
result = pd.DataFrame(res)
result.columns = ['address', 'longitude', 'latitude']
print(result)
result.to_excel('result.xlsx', index = False)
Out:
address longitude latitude
0 北京市朝阳区 116.601144 39.948574
1 北京市海淀区 116.329519 39.972134
2 上海市黄浦区 121.469240 31.229860
3 上海市虹口区 121.505133 31.264600
But how could I get the final result as follows? Thanks for your kind help at advance.
id name address longitude latitude
0 1 ABC tower 北京市朝阳区 116.601144 39.948574
1 2 AC park 北京市海淀区 116.329519 39.972134
2 3 ZR hospital 上海市黄浦区 121.469240 31.229860
3 4 Fengtai library NaN NaN NaN
4 5 Square Point 上海市虹口区 121.505133 31.264600
use pd.merge, as result is the longitude & latitude dataframe.
dfn = pd.merge(df, result, on='address', how='left')
or
for _, row in df.iterrows():
_id = row['id']
name = row['name']
addre = row['address']
if pd.isna(row['address']):
res.append([_id, name, addre, None, None])
continue
###### same code ######
url = '...'
# ...
###### same code ######
res.append([_id, name, addre, lon, lat])
result = pd.DataFrame(res)
result.columns = ['id', 'name', 'address', 'longitude', 'latitude']
print(result)
result.to_excel('result.xlsx', index = False)

Python , how to get value from a loop and assign to another loop?

I have two loops below the first one is the timand and the second one is shared. What i want to know how can i assign each result to shared["score"] ? cause what i had try below i assign shared["score"] = timang["score"] just return 1 1 1 .... And also how can we return multiple response in python for example
return Response(shared_data, tomon_dat, status=status.HTTP_200_OK) is this possible?
#result of timang
Result: 0
Result: 1
Result: 0
Result: 0
Result: 1
Result: 1
for timang in tomon_dat:
tm_ins = QuestionaireAnswerModel.objects.get(id=timang["id"])
timang["score"] = tm_ins.score
timang["id"] = tm_ins.id
datatest = timang["score"]
for shared in shared_data:
questionaire_ins = QuestionaireModel.objects.get(random_code=shared["random_code"])
shared["title"] = questionaire_ins.title
shared["sub_title"] = questionaire_ins.sub_title
shared["idddd"] = questionaire_ins.id
answer_ins = SharedQuestionaire.objects.get(id=shared["id"])
shared["is_answered"] = (QuestionaireAnswerModel.objects.filter(shared_questionaire=answer_ins).count()) > 0
shared["score"] = timang["score"]

How to split a comma-delimited list in IronPython (Spotfire)?

I have a existing data table with two columns, one is a ID and one is a list of IDs, separated by comma.
For example
ID | List
---------
1 | 1, 4, 5
3 | 2, 12, 1
I would like to split the column List so that I have a table like this:
ID | List
---------
1 | 1
1 | 4
1 | 5
3 | 2
3 | 12
3 | 1
I figured this out now:
tablename='Querysummary Data'
table=Document.Data.Tables[tablename]
topiccolname='TOPIC_ID'
topiccol=table.Columns[topiccolname]
topiccursor=DataValueCursor.Create[str](topiccol)
docscolname='DOC_IDS'
doccol=table.Columns[docscolname]
doccursor=DataValueCursor.Create[str](doccol)
myPanel = Document.ActivePageReference.FilterPanel
idxSet = myPanel.FilteringSchemeReference.FilteringSelectionReference.GetSelection(table).AsIndexSet()
keys=dict()
topdoc=dict()
for row in table.GetRows(idxSet,topiccursor,doccursor):
keys[topiccursor.CurrentValue]=doccursor.CurrentValue
for key in keys:
str = keys[key].split(",")
for i in str:
topdoc[key]=i
print key + " " +i
now I can print the topic id with the corresponding id.
How can I create a new data table in Spotfire using this dict()?
I solved it myself finally..maybe there is some better code but it works:
tablename='Querysummary Data'
table=Document.Data.Tables[tablename]
topiccolname='TOPIC_ID'
topiccol=table.Columns[topiccolname]
topiccursor=DataValueCursor.Create[str](topiccol)
docscolname='DOC_IDS'
doccol=table.Columns[docscolname]
doccursor=DataValueCursor.Create[str](doccol)
myPanel = Document.ActivePageReference.FilterPanel
idxSet = myPanel.FilteringSchemeReference.FilteringSelectionReference.GetSelection(table).AsIndexSet()
# build a string representing the data in tab-delimited text format
textData = "TOPIC_ID;DOC_IDS\r\n"
keys=dict()
topdoc=dict()
for row in table.GetRows(idxSet,topiccursor,doccursor):
keys[topiccursor.CurrentValue]=doccursor.CurrentValue
for key in keys:
str = keys[key].split(",")
for i in str:
textData += key + ";" + i + "\r\n"
dataSet = DataSet()
dataTable = DataTable("DOCIDS")
dataTable.Columns.Add("TOPIC_ID", System.String)
dataTable.Columns.Add("DOC_IDS", System.String)
dataSet.Tables.Add(dataTable)
# make a stream from the string
stream = MemoryStream()
writer = StreamWriter(stream)
writer.Write(textData)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)
# set up the text data reader
readerSettings = TextDataReaderSettings()
readerSettings.Separator = ";"
readerSettings.AddColumnNameRow(0)
readerSettings.SetDataType(0, DataType.String)
readerSettings.SetDataType(1, DataType.String)
readerSettings.SetDataType(2, DataType.String)
# create a data source to read in the stream
textDataSource = TextFileDataSource(stream, readerSettings)
# add the data into a Data Table in Spotfire
if Document.Data.Tables.Contains("Querysummary Mapping"):
Document.Data.Tables["Querysummary Mapping"].ReplaceData(textDataSource)
else:
newTable = Document.Data.Tables.Add("Querysummary Mapping", textDataSource)
tableSettings = DataTableSaveSettings (newTable, False, False)
Document.Data.SaveSettings.DataTableSettings.Add(tableSettings)

aligning, spacing in python 3

What can I do to align all columns in this code?, is that correct or...?
import urllib.request
from re import findall
def determinarLlegadas(numero):
llegadas = urllib.request.urlopen("http://...")
llegadas = str (llegadas.read())
llegadas = findall ('<font color="Black" size="3">(.+?)</font>',llegadas)
print ('\t','VUELO ','\t','AEROLINEA','\t','PROCEDENCIA','\t','FECHA ','\t',' HORA ','\t','ESTADO','\t','PUERTA')
a = 0
numero = numero * 7
while numero > a:
print ('\t',llegadas[a+0],'\t',llegadas[a+1],'\t',llegadas[a+3],'\t',llegadas[a+3],'\t',llegadas[a+4],'\t',llegadas[a+5],'\t',llegadas[a+6])
a = a + 7
Don't use tabs, use string formatting.
...
print("{:12}{:12}{:12}{:12}{:12}{:12}{:12}".format(
"VUELO","AEROLINEA","PROCEDENCIA","FECHA","HORA","ESTADO","PUERTA"))
print("{:12}{:12}{:12}{:12}{:12}{:12}{:12}".format(*llegadas))
Change the 12 to the maximum field size for each column, and you're golden.
In fact, though it's less readable:
COLSIZE = 12
# Maybe COLSIZE = max(map(len,llegadas))+1
NUMCOLS = 7
formatstring = "{}{}{}".format("{:",COLSIZE,"}")*NUMCOLS
# {:COLSIZE}*NUMCOLS
headers = ["VUELO","AEROLINEA","PROCEDENCIA","FECHA","HORA","ESTADO","PUERTA"]
print(formatstring.format(*headers))
print(formatstring.format(*llegadas))

Categories

Resources