Mar-13-2019, 12:27 PM
(This post was last modified: Mar-13-2019, 01:06 PM by Prince_Bhatia.)
hi,
I have a csv which has two columns and on the other hand i have multiple lists in my code.
I am looping through second column of each data in csv and checking if it exists in the already defined lists.
if it exists create a new csv and add them. But what has happened till now it has added data but it is separating them into different columns and i want it data of each category into one column but comma separated.
below is my code:
and attached is the output csv how i want data to be which is by name "new_facilities.csv"
Please help!
I have a csv which has two columns and on the other hand i have multiple lists in my code.
I am looping through second column of each data in csv and checking if it exists in the already defined lists.
if it exists create a new csv and add them. But what has happened till now it has added data but it is separating them into different columns and i want it data of each category into one column but comma separated.
below is my code:
import csv from itertools import groupby,chain from operator import itemgetter Facility1 = ["club_house", "gymnasium", "children’s_play_area", "24/7_water_supply", "power_back-up", "inter-com","lift"] Facility2 = ["jogging_track", "swimming_pool", "aerobics_center", "badminton_court", "basketball_court", "beach_volleyball_court", "football_court", "golf_course", "lawn_tennis_court","yoga/meditation_center"] Facility3 = ["crèche", "ATM", "car_wash_area", "community_hall", "banquet_hall", "garbage_disposal", "grocery_shop","library"] Facility4 = ["fountains","landscape_gardens"] Facility5 = ["gated_society"] Facility6 = ["car_parking"] Facility7 = ["CCTV_cameras"] Facility8 = ["security_guards"] Facility9 = ["pool_table", "carrom_room", "chess_room", "dart_board", "squash_court", "table_tennis_room", "skating_rink"] def main(): prj_structure = {} f = open("./facility_data.csv","r",encoding="utf-8") data = f.read() f.close() lst = data.split("\n") prj = "" for i in range(1, len(lst)): val = lst[i].split(",") if len(val)>0: prj = val[0] if prj!="": if prj not in prj_structure.keys(): prj_structure[prj] = [] prj_structure[prj].append(val[1]) return prj_structure final_data = [] def readingfiles(): global final_data data = main() for k,v in data.items(): for i in v: sublist = [] sublist.append(k) if i in Facility1: sublist.append(i+"-facility1") if i in Facility2: sublist.append(i+"-facility2") if i in Facility3: sublist.append(i+"-facility3") if i in Facility4: sublist.append(i+"-facility4") if i in Facility5: sublist.append(i+"-facility5") if i in Facility6: sublist.append(i+"-facility6") if i in Facility7: sublist.append(i+"-facility7") if i in Facility8: sublist.append(i+"-facility8") if i in Facility9: sublist.append(i+"-facility9") final_data.append(sublist) return final_data def writefile(filename, alldata): global final_data with open(filename,"w", encoding="utf-8")as csvfile: csvfile = csv.writer(csvfile, delimiter=",") csvfile.writerow("") for i in range(0, len(alldata)): csvfile.writerow(alldata[i]) def get_new(): readingfiles() writefile("new_facilities.csv",[[k, *chain.from_iterable(r for _, *r in g)] for k, g in groupby(final_data, key=itemgetter(0))]) get_new()attached is the sample csv that i am using to create a new csv which is by name facility_data.csv
and attached is the output csv how i want data to be which is by name "new_facilities.csv"
Please help!




Attached Files

