大佬教程收集整理的这篇文章主要介绍了Python:将降价表转换为 json,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想弄清楚,仅使用 python 将一些 Markdown 表格文本转换为 Json 的最简单方法是什么。例如,将其视为输入字符串:
| Some title | Some Description | Some number |
|------------|------------------------------|-------------|
| Dark Souls | This is a fun game | 5 |
| Bloodborne | This one is even better | 2 |
| Sekiro | This one is also pretty good | 110101 |
想要的输出应该是这样的:
[
{"Some title":"Dark Souls","Some Description":"This is a fun game","Some number":5},{"Some title":"Bloodborne","Some Description":"This one is even better","Some number":2},{"Some title":"Sekiro","Some Description":"This one is also pretty good","Some number":110101}
]
注意:理想情况下,输出应符合 RFC 8259,也就是在键值对周围使用双引号 " 而不是单引号 '。
我已经看到一些 Js 库可以做到这一点,但仅适用于 python。 有人可以向我解释实现这一目标的最快方法是什么,所以我不必为此编写自己的解析器。
感谢所有帮助!
您可以将其视为多行字符串并逐行解析,同时在 import time
import pandas as pd
import multiprocessing as mp
def multiproc():
processes = []
setTings = {1: {'sleep_time': 5,'id': 1},2: {'sleep_time': 1,'id': 2},3: {'sleep_time': 2,'id': 3},4: {'sleep_time': 3,'id': 4}}
for key in setTings:
p = mp.process(target=calc_something,args=(setTings[key],))
processes.append(p)
p.start()
for p in processes:
p.join()
return processes
def calc_something(setTings: Dict) -> pd.DataFrame:
time_to_sleep = setTings['sleep_time']
time.sleep(time_to_sleep)
print(str(setTings['id']))
df = some_function_creates_data_frame()
return df
和 \n
处拆分
执行此操作的简单代码:
|
import json
my_str='''| Some title | Some Description | Some number |
|------------|------------------------------|-------------|
| Dark Souls | This is a fun game | 5 |
| Bloodborne | This one is even better | 2 |
| Sekiro | This one is also pretty good | 110101 |'''
def mrkd2json(inp):
lines = inp.split('\n')
ret=[]
keys=[]
for i,l in enumerate(lines):
if i==0:
keys=[_i.Strip() for _i in l.split('|')]
elif i==1: conTinue
else:
ret.append({keys[_i]:v.Strip() for _i,v in enumerate(l.split('|')) if _i>0 and _i<len(keys)-1})
return json.dumps(ret,indent = 4)
print(mrkd2json(my_str))
PS:不知道有什么图书馆可以做到这一点,如果我发现任何东西会更新!
,我的方法与@Kuldeep Singh Sidhu 的方法非常相似:
md_table = """
| Some title | Some Description | Some number |
|------------|------------------------------|-------------|
| Dark Souls | This is a fun game | 5 |
| Bloodborne | This one is even better | 2 |
| Sekiro | This one is also pretty good | 110101 |
"""
result = []
for n,line in enumerate(md_table[1:-1].split('\n')):
data = {}
if n == 0:
header = [t.Strip() for t in line.split('|')[1:-1]]
if n > 1:
values = [t.Strip() for t in line.split('|')[1:-1]]
for col,value in zip(header,values):
data[col] = value
result.append(data)
结果是:
[{'Some title': 'Dark Souls','Some Description': 'This is a fun game','Some number': '5'},{'Some title': 'Bloodborne','Some Description': 'This one is even better','Some number': '2'},{'Some title': 'Sekiro','Some Description': 'This one is also pretty good','Some number': '110101'}]
@H_489_53@
以上是大佬教程为你收集整理的Python:将降价表转换为 json全部内容,希望文章能够帮你解决Python:将降价表转换为 json所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。