Mar-14-2019, 04:45 AM
(This post was last modified: Mar-14-2019, 04:45 AM by Prince_Bhatia.)
but how can i add them into one column?Please explain the concept also. Please check my output file like how i want my output to be
Reading and manipulating csv
|
Mar-14-2019, 04:45 AM
(This post was last modified: Mar-14-2019, 04:45 AM by Prince_Bhatia.)
but how can i add them into one column?Please explain the concept also. Please check my output file like how i want my output to be
There may be an item or two that needs tweaking, (need to eliminate None items) but it's very close:
import csv from itertools import groupby,chain from operator import itemgetter import os facilities = { '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(): # Make sure in src directory to start os.chdir(os.path.abspath(os.path.dirname(__file__))) save_id = None outvals = [] with open("facility_data.csv", encoding="ISO-8859-1") as fp, open('new_facilities.csv', 'w') as fo: crdr = csv.reader(fp, delimiter=',') for row in crdr: id = row[0] if save_id == None: save_id = id rvalue = row[1] if id != save_id: if save_id is not None: if len(outvals): outstr = ','.join(outvals) outstr = f'{save_id},{outstr}' else: outstr = f'{save_id},None' fo.write(f'{outstr}\n,,\n') outvals = [] save_id = id for facid, values in facilities.items(): if rvalue in values: outvals.append(f'{rvalue}-{facid}') if __name__ == '__main__': main()partial results:
|
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
Manipulating data from a CSV | EvanS1 | 5 | 3,660 |
Jun-12-2020, 05:59 PM Last Post: perfringo |
|
manipulating two lists | rancans | 8 | 4,588 |
Apr-16-2020, 06:00 PM Last Post: deanhystad |
|
Manipulating index value, what is wrong with this code? | Emun | 1 | 2,410 |
Feb-05-2020, 07:18 AM Last Post: perfringo |
|
Manipulating the filename of an output script | mckinneycm | 4 | 13,072 |
Jan-15-2020, 07:29 PM Last Post: mckinneycm |
|
Manipulating Excel with Python. | Spacely | 2 | 5,181 |
Jun-25-2019, 01:57 AM Last Post: Dequanharrison |
|
Manipulating CSV | Prince_Bhatia | 1 | 2,477 |
Apr-25-2019, 11:55 AM Last Post: Gribouillis |
|
Manipulating an Excel Workbook | Stanimal | 4 | 4,122 |
Jan-18-2019, 11:03 PM Last Post: Stanimal |
|
Manipulating Binary Data | arsenal88 | 10 | 10,395 |
Apr-25-2017, 02:30 PM Last Post: snippsat |
|
Manipulating files Python 2.7 | hugobaur | 6 | 11,253 |
Nov-01-2016, 12:28 PM Last Post: hugobaur |