我正在尝试将字典导出到 csv。我正在从 api 中提取数据并需要将其打印到 CSV。
我在用:
import datetime
import csv
import pendulum
import requests
from tabulate import tabulate
import pandas as pd
import numpy as np
我调用的 API 如下:
api_url = "https://secure-webtv-static.canal-plus.com/metadata/cpfra/all/v2.2/globalchannels.json"
response = requests.get(api_url).json()
获取我想提取的数据在这里:
tv_programme = {
channel["name"]: [
[
e['title'],
e['subTitle'],
pendulum.parse(e['timecodes'][0]['start']
).time().strftime("%H:%M"),
datetime.timedelta(
milliseconds=e['timecodes'][0]['duration'],
).__str__().rsplit(".")[0],
] for e in channel["events"]
] for channel in response["channels"]
}
我正在尝试使用 Pandas 将数据发送到CSV
文件
df = pd.DataFrame(tabulate(
tv_programme["CANAL+ SPORT"],
headers="firstrow"("Title", "Subtitle", "Time", "Duration"),
tablefmt="csv",
))
print(tabulate(
tv_programme["CANAL+ SPORT"],
headers=["Title", "Subtitle", "Time", "Duration"],
tablefmt="csv",
))
df = pd.DataFrame(sorted(list(tv_programme.headers('sports.csv'))))
收集print
以下数据,我需要它来填充CSV
文件。
Title Subtitle Time Duration
----------------------------------- ----------- ------ ----------
Sport Reporter Doc Sport 10:42 0:26:23
Chelsea / West Ham 14e journée 11:11 0:48:38
Cesta punta - Pro Tour 2020 Autre Sport 12:51 1:28:53
Rugby - Golden Lions / Natal Sharks 4e journée 14:20 0:45:56
Rugby - Colomiers / Perpignan 8e journée 15:55 0:50:00
Rugby - Oyonnax / Biarritz 6e journée 17:55 0:50:00
Rugby - Castres / Brive 4e journée 19:55 1:15:00
现在这是我卡住并收到错误的地方:TypeError: 'str' object is not callable