101_文化部展覽資訊

題目說明:

請開啟PYD01.py檔案,依下列題意進行作答,使輸出值符合題意要求。作答完成請另存新檔為PYA01.py再進行評分。

程式所產出的檔案,須輸出與程式同一層資料夾。

設計說明:

請撰寫一程式,讀取文化部展覽資訊read.json,請將其中title(活動名稱)、showUnit(演出單位)、startDate(活動起始日期)、endDate(活動結束日期)等四個欄位內容轉存為write.csv (需為UTF-8編碼格式),各欄位內容之間以一個半形逗號隔開。

提示:只需要輸出資料,不需要輸出欄位名稱。

輸入輸出:

輸入說明

讀取read.json

輸出說明

將四個欄位的內容:title、showUnit、startDate、endDate,輸出至write.csv檔案,各欄位內容之間以一個半形逗號隔開

輸入輸出範例:

範例輸入_1

範例輸出_1

1637812686517

待編修檔案:

# 載入 json 與 csv 模組
import ___
import ___

# 讀取 json 檔案並指定編碼為 utf8
with ___("___", encoding='___') as file:
    data = json.load(file)

# 寫入 csv 檔案並指定編碼為 utf8
with ___("___", "___", encoding='___') as file:
    csv_file = csv.writer(file)
    # 寫入 title(活動名稱)、showUnit(演出單位)、startDate(活動起始日期)、endDate(活動結束日期)等四個欄位
    for item in data:
        csv_file.writerow([___['___'], ___['___'],
                           ___['___'], ___['___']])

程式碼_1:

# 載入 json 與 csv 模組
import csv
import json


with open(r'read.json',encoding='utf-8') as f:
    json_data=json.load(f)
    
columns=['title','showUnit','startDate','endDate']

datas=[[data[column] for column in columns] for data in json_data]

with open("write.csv", "w",newline='',encoding='utf-8') as file:
    csv_file = csv.writer(file)
    csv_file.writerows(datas)

程式碼_2:

# 載入 json 與 csv 模組
import json
import csv

# 讀取 json 檔案並指定編碼為 utf8
with open("./read.json", encoding='utf8') as file:
    data = json.load(file)

# 寫入 csv 檔案並指定編碼為 utf8
with open("write.csv", "w", encoding='utf8') as file:
    csv_file = csv.writer(file)
    # 寫入 title(活動名稱)、showUnit(演出單位)、startDate(活動起始日期)、endDate(活動結束日期)等四個欄位
    for item in data:
        csv_file.writerow([item['title'], item['showUnit'],
                           item['startDate'], item['endDate']])

 

程式碼_3:

# 載入 json 與 csv 模組
import json
import csv

# 讀取 json 檔案並指定編碼為 utf8
with open(r"read.json", encoding='utf8') as file:
    data = json.load(file)

# 寫入 csv 檔案並指定編碼為 utf8
with open("write.csv", "w", encoding='utf8') as file:
    csv_file = csv.writer(file)
    # 寫入 title(活動名稱)、showUnit(演出單位)、startDate(活動起始日期)、endDate(活動結束日期)等四個欄位
    for item in data:
        csv_file.writerow([item['title'], item['showUnit'],
                           item['startDate'], item['endDate']])


 搭 配 學 習 

  • Python 3.x 網頁資料擷取與分析特訓教材風_碁峰

    博客來      誠品

  • TQC+ Python 3.x網頁資料擷取與分析特訓教材_全華

    博客來      誠品

 

​​​​​​​

arrow
arrow
    創作者介紹
    創作者 DING BANG 的頭像
    DING BANG

    DING BANG的部落格

    DING BANG 發表在 痞客邦 留言(3) 人氣()