简体   繁体   中英

Unable to write data across columns in a csv file

I've written a script in python to scrape different names and their values out of a table from a webpage and write the same in a csv file. My below script can parse them flawlessly but I can't write them to a csv file in a customized manner.

What I wish to do is write the names and values across columns which you may see in image 2.

This is my try:

import csv
from bs4 import BeautifulSoup
import requests

res = requests.get("https://www.bloomberg.com/markets/stocks",headers={"User-Agent":"Mozilla/5.0"})
soup = BeautifulSoup(res.text, "lxml")
with open("outputfile.csv","w",newline="") as infile:
    writer = csv.writer(infile)
    for table in soup.select(".data-table-body tr"):
        name = table.select_one("[data-type='full']").text
        value = table.select_one("[data-type='value']").text
        print(f'{name} {value}')
        writer.writerow([name,value])

Output I'm getting like below:

在此处输入图片说明

How I wish to get the output is like the following:

在此处输入图片说明

Any help to solve this will be vastly appreciated.

Try to define empty list, append all the values in a loop and then write them all at once:

with open("outputfile.csv","w",newline="") as infile:
    writer = csv.writer(infile)
    names_and_values = []
    for table in soup.select(".data-table-body tr"):
        name = table.select_one("[data-type='full']").text
        value = table.select_one("[data-type='value']").text
        print(f'{name} {value}')
        names_and_values.extend([name,value])
    writer.writerow(names_and_values)

If I understand you correctly, try making just one call to writerow instead of one per loop

import csv
from bs4 import BeautifulSoup
import requests

res = requests.get("https://www.bloomberg.com/markets/stocks",headers={"User-Agent":"Mozilla/5.0"})
soup = BeautifulSoup(res.text, "lxml")
with open("outputfile.csv","w",newline="") as infile:
    writer = csv.writer(infile)
    data = []
    for table in soup.select(".data-table-body tr"):
        name = table.select_one("[data-type='full']").text
        value = table.select_one("[data-type='value']").text
        print(f'{name} {value}')
        data.extend([name, value])
    writer.writerow(data)

That seems like an ugly thing to want to do, are you sure?

Use pandas for getting csvs and manipulating tables. You'll want to do something like:

import pandas as pd

df = pd.read_csv(path)
df.values.ravel()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM