0

I've got a dictionary with multiple rows from my question before. It's generated via:

import requests
from bs4 import BeautifulSoup
import re
import csv

links = ["34519-kevitsa-copper-concentrator-plant", "34520-kevitsa-copper-mine", "34356-glogow-copper-refinery"]

for l in links:

    page = requests.get("https://www.industryabout.com/country-territories-3/2199-finland/copper-mining/"+l)
    soup = BeautifulSoup(page.content, 'lxml')
    rows = soup.select("strong")
    d = {}
    
    for r in rows:
        name, value, *rest = r.text.split(":")
        if not rest:
            d[name] = value
    print(d)

print(d.items()) gives:

dict_items([('Commodities', ' Copper, Nickel, Gold'), ('Area', ' Lappi'), ('Type', ' Copper Concentrator Plant') ])

dict_items([('Commodities', ' Copper, Nickel, Gold'), ('Area', ' Lappi'), ('Type', ' Open-pit Mine') ])

dict_items([('Commodities', ' Copper'), ('Area', ' Dolnoslaskie'), ('Type', ' Copper Refinery') ])

But when I write this to CSV with any of the solutions from "Writing a dictionary to a csv file with one line for every 'key: value'" I only get the LAST dictionary item, the others are omitted.

Commodities;Copper
Area;Dolnoslaskie
Type;Copper Refinery

How can I write a CSV with looks like this:

Commodities;Area;Type
Copper, Nickel, Gold;Lappi;Copper Concentrator Plant
Copper, Nickel, Gold;Lappi;Open-pit mine
Copper;Dolnoslaskie;Copper Refinery

I've also tried to change to an array, but coulnd't find a solution for array[name] = value.

This answer seems perfect, but iterkeys is Python 2 only.

with open('my_data.csv', 'wb') as ofile:
    writer = csv.writer(ofile, delimiter='\t')
    writer.writerow(['ID', 'dict1', 'dict2', 'dict3'])
    for key in ddict.iterkeys():
        writer.writerow([key] + [d[key] for d in dicts])
Community
  • 1
  • 1
pickenpack
  • 87
  • 10
  • 1
    Your question starts out talking about one dictionary, and then suddenly there's multiple. Please include more code. – Alex Hall May 05 '18 at 22:54
  • Here is the full code. – pickenpack May 06 '18 at 08:44
  • The question "Writing a dictionary to a csv file with one line for every 'key: value'" is exactly what you don't want though. You want to write several dictionaries, with a line per dictionary. DictWriter will do great. – Alex Hall May 06 '18 at 08:57
  • It only works in Python2 (see above). – pickenpack May 06 '18 at 10:05
  • Again, that answer is for a question where the asker's desired output is the opposite of what you want. They want a column for each dict, you want a row for each dict. – Alex Hall May 06 '18 at 10:11
  • I don't care. A column for each dict also would be ok. – pickenpack May 06 '18 at 10:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170457/discussion-between-alex-hall-and-pickenpack). – Alex Hall May 06 '18 at 10:55

2 Answers2

0
import csv

with open('data.csv', 'w') as outfile:
    writer = csv.DictWriter(outfile, ['Commodities', 'Area', 'Type'], delimiter=';')
    writer.writeheader()
    for l in links:
        ...
        print(d)
        writer.writerow(d)
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
-1

You can try this.

#!python
# -*- coding: utf-8 -*-#

d = {k:v for (k,v) in zip(list('abc'),range(3))}
print(d)

with open('a.txt','w') as fo:
    for i in d:
        line = '{};{}\n'.format(i,d[i])
        fo.write(line)
BhishanPoudel
  • 15,974
  • 21
  • 108
  • 169