簡體   English   中英

Output 結果為 csv 文件

[英]Output results as csv file

你能幫我 output 結果到 csv 文件嗎?

import requests
import xml.etree.ElementTree as ET
import datetime

response = requests.get('https://publicacionexterna.azurewebsites.net/publicaciones/prices')
myroot = ET.fromstring(response.content)

print(f"Place_id\tItem\tPrice\tDate")

for x in myroot.findall('place'):
         place_id = x.get('place_id')
         item = x.find('gas_price').attrib['type']
         price = x.find('gas_price').text
         today = datetime.date.today()
         print(f"{place_id}\t{item}\t{price}\t{today}")

感謝您的幫助

  • 創建一個 header 列表 創建行列表
  • 將每一行作為列表添加到 dataList
  • 打開文件並寫入頭列表
  • 將行數據寫入打開的文件

Main.py

import csv
import requests
import xml.etree.ElementTree as ET
import datetime

response = requests.get('https://publicacionexterna.azurewebsites.net/publicaciones/prices')
myroot = ET.fromstring(response.content)

print(f"Place_id\tItem\tPrice\tDate")

headerlist = ["Place_id","Item","Price","Date"]  # added a header list

dataList = [] # empty list to place each row

for x in myroot.findall('place'):
    rowList =[] # empty row list to add individual data
    
    place_id = x.get('place_id')
    rowList.append(place_id)  # append place id
    item = x.find('gas_price').attrib['type']
    rowList.append(item) # append item
    price = x.find('gas_price').text
    rowList.append(price) # append price
    today = datetime.date.today()
    rowList.append(today) # append today
    dataList.append(rowList) # append the entire row as a lit to data list


with open('example.csv', 'w', newline='') as file: # open csv file 
      write = csv.writer(file)
      write.writerow(headerlist) # write header data
      write.writerows(dataList) # write each row

您可以使用列表理解來構建字典列表,並將其傳遞給pandas dataframe

import pandas as pd

data = [{
  "place_id": x.get('place_id'),
  "item": x.find('gas_price').attrib['type'],
  "price": x.find('gas-price').text,
  "today": datetime.date.today()
}, for x in myroot.findall('place')]

df = pd.DataFrame(data)
df.to_csv("Output_csv_file.csv")

缺點是您必須安裝第三方庫 (pandas),但這是一個簡單的高級解決方案,希望對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM