如何编写python代码批量转换json到html?
How to write python code batch convert json to html?
我有 300 + json 个文件。
结构是这样的:
{
"body_html": "<div><head></head><body><div class=\"lake-content-editor-core lake-engine lake-typography-traditional\" data-lake-element=\"root\"><p id=\"a596b3f2-13d2-4a99-a3c2-5e0952e0b600\" style=\"font-size: 14px; color: rgb(38, 38, 38); line-height: 1.74; letter-spacing: 0.05em; outline-style: none; overflow-wrap: break-word; margin: 0px;\"><a href=\"https://ideas.darden.virginia.edu/the-power-of-an-idea-meritocracy\" target=\"_blank\">https://ideas.darden.virginia.edu/the-power-of-an-idea-meritocracy</a></p><p style=\"font-size: 14px; color: rgb(38, 38, 38); line-height: 1.74; letter-spacing: 0.05em; outline-style: none; overflow-wrap: break-word; margin: 0px;\"><br></p><p id=\"a596b3f2-13d2-4a99-a3c2-5e0952e0b600\" style=\"font-size: 14px; color: rgb(38, 38, 38); line-height: 1.74; letter-spacing: 0.05em; outline-style: none; overflow-wrap: break-word; margin: 0px;\">if you want to hire great people and have them stay working for you. you have to let them make a lot of decisions , and<mark> You have to be run by ideas, not hierarchy. the best ideas have to win , otherwise good people don't stay</mark> <a href=\"https://www.google.com/search?newwindow=1&ei=XxREXteKHZWe4-EP7t204Aw&q=idea+meritocracy&oq=ideameritocracy&gs_l=psy-ab.1.0.0i10l4.870029.873986..875653...0.2..0.273.273.2-1......1....2j1..gws-wiz.......0i71.4KOME2IXHps\" target=\"_blank\">#ideameritocracy</a></p><p id=\"0798738b-9fb5-4ff3-9ff8-b6e395dd642b\" style=\"font-size: 14px; color: rgb(38, 38, 38); line-height: 1.74; letter-spacing: 0.05em; outline-style: none; overflow-wrap: break-word; margin: 0px;\">To be successful you need to pick product ideas based on their own merit (idea meritocracy) rather than on the basis of who thinks they are good (people meritocracy).</p><p id=\"949045a7-6b18-4fdf-af54-661dab3fca07\" style=\"font-size: 14px; color: rgb(38, 38, 38); line-height: 1.74; letter-spacing: 0.05em; outline-style: none; overflow-wrap: break-word; margin: 0px;\"><mark>An Idea Meritocracy is an environment in which the best idea wins</mark>. \n<mark>The best idea is determined by the quantity and quality of the data, not by positional power</mark>. \nI have studied examples of companies that have created Idea Meritocracies, including Google, Intuit, Pixar Animation Studios and Bridgewater Associates. In those organizations, an Idea Meritocracy has played a key role in driving consistent high performance and has warded off complacency and group think by empowering employees to have the curiosity and courage to challenge, to explore like scientists by asking the <mark>3 W</mark>’s: <mark>Why ? What if ? Why not ?</mark></p><blockquote style=\"padding-left: 1em; margin-top: 5px; margin-bottom: 5px; margin-left: 0px; border-left: 3px solid rgb(238, 238, 238); opacity: 0.6;\">To flourish in the innovation age, companies must change how decisions are made and change how leaders lead. To do so you must change how decisions are made to what I call leadership by experiment. Moving from politics and PowerPoints to enabling the idea to prove itself. From boss votes with their opinion, to the customers vote with their feet. From the hierarchy sets the agenda, to the innovators set the agenda.</blockquote></div></body></div>",
"slug": 4710687,
"title": "Idea Meritocracy"
}
如何编写python代码批量转换json到html?
html 文件名为“title”。例如“创意精英”。
示例 json 文件:https://www.dropbox.com/s/1dbnzvb99wrm0v0/json%20file%20-kangland.zip?dl=0
抱歉:我不是码农,我只是想了解代码逻辑,但我不会写代码。
非常感谢!
第 1 步:
pip install json2html
第 2 步:
from json2html import *
您可以读取目录中的单个 JSON 并将其传递到 for 循环中,目前,我已经读取了单个 JSON。
sample = {
"body_html": "<div><head></head><body><div class=\"lake-content-editor-core lake-engine\" data-lake-element=\"root\" data-selection...........}
json2html.convert(json = sample)
✅ 您的输出将如下所示:
import json
list_of_files = ["kiqgfg.json"]
for file_name in list_of_files:
fi = open(file_name, 'r')
data = json.load(fi)
fo = open(data["title"]+".html", 'w')
fo.write(data["body_html"])
fi.close()
fo.close()
它取每个文件并取数据写入一个新的html文件。
如果您需要读取特定目录中的所有 json 文件,请使用以下代码。 编辑:在 json 文件
中添加了密钥检查
import json
from glob import glob
for file_name in glob("*.json"):
fi = open(file_name, 'r')
data = json.load(fi)
if 'title' in data.keys():
fo = open(data["title"], 'w')
if 'body_html' in data.keys():
fo.write(data["body_html"])
fi.close()
fo.close()
我有 300 + json 个文件。
结构是这样的:
{
"body_html": "<div><head></head><body><div class=\"lake-content-editor-core lake-engine lake-typography-traditional\" data-lake-element=\"root\"><p id=\"a596b3f2-13d2-4a99-a3c2-5e0952e0b600\" style=\"font-size: 14px; color: rgb(38, 38, 38); line-height: 1.74; letter-spacing: 0.05em; outline-style: none; overflow-wrap: break-word; margin: 0px;\"><a href=\"https://ideas.darden.virginia.edu/the-power-of-an-idea-meritocracy\" target=\"_blank\">https://ideas.darden.virginia.edu/the-power-of-an-idea-meritocracy</a></p><p style=\"font-size: 14px; color: rgb(38, 38, 38); line-height: 1.74; letter-spacing: 0.05em; outline-style: none; overflow-wrap: break-word; margin: 0px;\"><br></p><p id=\"a596b3f2-13d2-4a99-a3c2-5e0952e0b600\" style=\"font-size: 14px; color: rgb(38, 38, 38); line-height: 1.74; letter-spacing: 0.05em; outline-style: none; overflow-wrap: break-word; margin: 0px;\">if you want to hire great people and have them stay working for you. you have to let them make a lot of decisions , and<mark> You have to be run by ideas, not hierarchy. the best ideas have to win , otherwise good people don't stay</mark> <a href=\"https://www.google.com/search?newwindow=1&ei=XxREXteKHZWe4-EP7t204Aw&q=idea+meritocracy&oq=ideameritocracy&gs_l=psy-ab.1.0.0i10l4.870029.873986..875653...0.2..0.273.273.2-1......1....2j1..gws-wiz.......0i71.4KOME2IXHps\" target=\"_blank\">#ideameritocracy</a></p><p id=\"0798738b-9fb5-4ff3-9ff8-b6e395dd642b\" style=\"font-size: 14px; color: rgb(38, 38, 38); line-height: 1.74; letter-spacing: 0.05em; outline-style: none; overflow-wrap: break-word; margin: 0px;\">To be successful you need to pick product ideas based on their own merit (idea meritocracy) rather than on the basis of who thinks they are good (people meritocracy).</p><p id=\"949045a7-6b18-4fdf-af54-661dab3fca07\" style=\"font-size: 14px; color: rgb(38, 38, 38); line-height: 1.74; letter-spacing: 0.05em; outline-style: none; overflow-wrap: break-word; margin: 0px;\"><mark>An Idea Meritocracy is an environment in which the best idea wins</mark>. \n<mark>The best idea is determined by the quantity and quality of the data, not by positional power</mark>. \nI have studied examples of companies that have created Idea Meritocracies, including Google, Intuit, Pixar Animation Studios and Bridgewater Associates. In those organizations, an Idea Meritocracy has played a key role in driving consistent high performance and has warded off complacency and group think by empowering employees to have the curiosity and courage to challenge, to explore like scientists by asking the <mark>3 W</mark>’s: <mark>Why ? What if ? Why not ?</mark></p><blockquote style=\"padding-left: 1em; margin-top: 5px; margin-bottom: 5px; margin-left: 0px; border-left: 3px solid rgb(238, 238, 238); opacity: 0.6;\">To flourish in the innovation age, companies must change how decisions are made and change how leaders lead. To do so you must change how decisions are made to what I call leadership by experiment. Moving from politics and PowerPoints to enabling the idea to prove itself. From boss votes with their opinion, to the customers vote with their feet. From the hierarchy sets the agenda, to the innovators set the agenda.</blockquote></div></body></div>",
"slug": 4710687,
"title": "Idea Meritocracy"
}
如何编写python代码批量转换json到html?
html 文件名为“title”。例如“创意精英”。
示例 json 文件:https://www.dropbox.com/s/1dbnzvb99wrm0v0/json%20file%20-kangland.zip?dl=0
抱歉:我不是码农,我只是想了解代码逻辑,但我不会写代码。
非常感谢!
第 1 步:
pip install json2html
第 2 步:
from json2html import *
您可以读取目录中的单个 JSON 并将其传递到 for 循环中,目前,我已经读取了单个 JSON。
sample = {
"body_html": "<div><head></head><body><div class=\"lake-content-editor-core lake-engine\" data-lake-element=\"root\" data-selection...........}
json2html.convert(json = sample)
✅ 您的输出将如下所示:
import json
list_of_files = ["kiqgfg.json"]
for file_name in list_of_files:
fi = open(file_name, 'r')
data = json.load(fi)
fo = open(data["title"]+".html", 'w')
fo.write(data["body_html"])
fi.close()
fo.close()
它取每个文件并取数据写入一个新的html文件。
如果您需要读取特定目录中的所有 json 文件,请使用以下代码。 编辑:在 json 文件
中添加了密钥检查import json
from glob import glob
for file_name in glob("*.json"):
fi = open(file_name, 'r')
data = json.load(fi)
if 'title' in data.keys():
fo = open(data["title"], 'w')
if 'body_html' in data.keys():
fo.write(data["body_html"])
fi.close()
fo.close()