I am doing an assignment in which I need to open a raw mailing list, saved in a csv file, filter the users that have been unsubscribed, and print back the resulting mailing list to another csv file. To do so, I first need to create tuples with each row in the original list, and then transform the tuples into a dictionary. Any help filling out the blank parts of this code would be much appreciated!
mailing_list.csv:
uuid,username,email,subscribe_status
307919e9-d6f0-4ecf-9bef-c1320db8941a,afarrimond0,[email protected],opt-out
8743d75d-c62a-4bae-8990-3390fefbe5c7,tdelicate1,[email protected],opt-out
68a32cae-847a-47c5-a77c-0d14ccf11e70,edelahuntyk,[email protected],OPT-OUT
a50bd76f-bc4d-4141-9b5d-3bfb9cb4c65d,tdelicate10,[email protected],active
26edd0b3-0040-4ba9-8c19-9b69d565df36,ogelder2,[email protected],unsubscribed
5c96189f-95fe-4638-9753-081a6e1a82e8,bnornable3,[email protected],opt-out
480fb04a-d7cd-47c5-8079-b580cb14b4d9,csheraton4,[email protected],active
d08649ee-62ae-4d1a-b578-fdde309bb721,tstodart5,[email protected],active
5772c293-c2a9-41ff-a8d3-6c666fc19d9a,mbaudino6,[email protected],unsubscribed
9e8fb253-d80d-47b5-8e1d-9a89b5bcc41b,paspling7,[email protected],active
055dff79-7d09-4194-95f2-48dd586b8bd7,mknapton8,[email protected],active
5216dc65-05bb-4aba-a516-3c1317091471,ajelf9,[email protected],unsubscribed
41c30786-aa84-4d60-9879-0c53f8fad970,cgoodleyh,[email protected],active
3fd55224-dbff-4c89-baec-629a3442d8f7,smcgonnelli,[email protected],opt-out
2ac17a63-a64b-42fc-8780-02c5549f23a7,mmayoralj,[email protected],unsubscribed
import csv
base_url = '../dataset/'
def read_mailing_list_file():
with open('mailing_list.csv', 'r') as csv_file:
hdr = csv.Sniffer().has_header(csv_file.read())
csv_file.seek(0)
file_reader = csv.reader(csv_file)
line_count = 0
mailing_list = []
if hdr:
next(file_reader)
for row in file_reader:
mailing_list.append(row)
line_count = 1
mailing_list_buffer = # Create another list variable that will be used as a temporary buffer to transform
# our previous list into a dictionary, which is the data structure expected from the `update_mailing_list_extended`
# function
# Looping through the mailing list object
for item in mailing_list:
# Creating tuples with each row in the original list
mailing_dict = # Transforming the list of tuples into a python dictionary
I am trying to transform a list of tuples into a dictionary.
CodePudding user response:
This seems like an XY problem - you are trying to solve problem X (filter csv) with solution Y (a dictionary) when there is a better way to solve X.
Going from the description of your problem, there is no need for a dictionary. You can filter the CSV row by row and write directly to the new file.
with open("mailing_list.csv") as infile:
with open("mailing_list_filtered.csv", "w") as outfile:
csv.writer(outfile).writerows(row for row in csv.reader(infile)
if row[0] != "unsubscribed")
CodePudding user response:
you can try to use dict class , such as my_dict = dict(tuple_list)