提问人:John Stendreen 提问时间:10/30/2023 最后编辑:decezeJohn Stendreen 更新时间:10/30/2023 访问量:33
蟒蛇3.该函数未从 txt 文件应用
Python3. The function is not applied from the txt file
问:
我有一个为行着色的函数,它在 .py 文件中工作正常。 我想将函数传输到 txt 文件,以便用户可以更改它,但是在读取 txt 文件时,该函数不起作用。
import pandas as pd
from datetime import datetime
import numpy as np
name = ['Diego', 'Luis']
date2 = pd.to_datetime(['20:52:10', '20:50:10']).strftime('%H:%M:%S')
mark = ['0', '1']
df = pd.DataFrame({'Name':name,'Date_plan':date2,'mark':mark})
def highlight_val(row):
value = row.loc['mark']
if value == '1':
color = '#ff1100'
else:
color = ''
return ['background-color: {}'.format(color) for r in row]
st = df.style.apply(highlight_val, axis=1)
all_htm_text = st.render()
f= open('fileNew.html', 'w')
f.write(all_htm_text)
f.close()
下面是带有外部 txt 文件的代码:
import pandas as pd
from datetime import datetime
import numpy as np
name = ['Diego', 'Luis']
date2 = pd.to_datetime(['20:52:10', '20:50:10']).strftime('%H:%M:%S')
mark = ['0', '1']
df = pd.DataFrame({'Name':name,'Date_plan':date2,'mark':mark})
functext = ""
with open("C:/python/background color/backcolorfunc_1.txt", "r" ) as fp:
functext = str(fp.read())
print(functext)
def hightlight_multy(row):
ret = ["" for _ in row.index]
if row.mark == '1': "'"+functext+"'"
#ret[row.index.get_loc("Name")] = "background-color: blue" #! this row is in the txt file
return ret
st = df.style.apply(hightlight_multy, axis=1)
all_htm_text = ''
all_htm_text = st.render()
f= open('fileNew.html', 'w')
f.write(all_htm_text)
f.close()
为什么函数的主体不能从从 txt 文件中读取的变量中工作?
答: 暂无答案
评论