Python Forum
Reading and manipulating csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading and manipulating csv
#8
perfringo's codec is correct.
this is much easier done by using a dictionary:
example:
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__)))

    with open("facility_data.csv", encoding="ISO-8859-1") as fp:
        crdr = csv.reader(fp, delimiter=',')
        for n, row in enumerate(crdr):
            for facid, values in facilities.items():
                if row[1] in values:
                    print(f'found: from row: {n}, {row[1]} ({row[0]}) in {facid}')

if __name__ == '__main__':
    main()
partial output:
Output:
found: from row: 1, swimming_pool (R3) in Facility2 found: from row: 2, gymnasium (R3) in Facility1 found: from row: 6, swimming_pool (R5) in Facility2 found: from row: 7, gymnasium (R5) in Facility1 found: from row: 11, swimming_pool (R6) in Facility2 found: from row: 12, gymnasium (R6) in Facility1 found: from row: 16, swimming_pool (R9) in Facility2 found: from row: 17, gymnasium (R9) in Facility1 found: from row: 20, swimming_pool (R10) in Facility2 found: from row: 21, gymnasium (R10) in Facility1 found: from row: 22, swimming_pool (R703) in Facility2 found: from row: 23, gymnasium (R703) in Facility1 found: from row: 26, swimming_pool (R17) in Facility2 found: from row: 27, gymnasium (R17) in Facility1 found: from row: 30, swimming_pool (R18) in Facility2
Also (which I haven't done), write youor output incrementally as you read input
Reply


Messages In This Thread
Reading and manipulating csv - by Prince_Bhatia - Mar-13-2019, 12:27 PM
RE: Reading and manipulating csv - by Larz60+ - Mar-13-2019, 01:00 PM
RE: Reading and manipulating csv - by Prince_Bhatia - Mar-13-2019, 01:01 PM
RE: Reading and manipulating csv - by Larz60+ - Mar-13-2019, 01:03 PM
RE: Reading and manipulating csv - by Prince_Bhatia - Mar-13-2019, 01:06 PM
RE: Reading and manipulating csv - by Larz60+ - Mar-13-2019, 01:13 PM
RE: Reading and manipulating csv - by perfringo - Mar-13-2019, 02:17 PM
RE: Reading and manipulating csv - by Larz60+ - Mar-13-2019, 02:38 PM
RE: Reading and manipulating csv - by Prince_Bhatia - Mar-13-2019, 04:21 PM
RE: Reading and manipulating csv - by Larz60+ - Mar-13-2019, 05:17 PM
RE: Reading and manipulating csv - by Prince_Bhatia - Mar-14-2019, 04:45 AM
RE: Reading and manipulating csv - by Larz60+ - Mar-14-2019, 11:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Manipulating data from a CSV EvanS1 5 2,771 Jun-12-2020, 05:59 PM
Last Post: perfringo
  manipulating two lists rancans 8 3,292 Apr-16-2020, 06:00 PM
Last Post: deanhystad
  Manipulating index value, what is wrong with this code? Emun 1 1,806 Feb-05-2020, 07:18 AM
Last Post: perfringo
  Manipulating the filename of an output script mckinneycm 4 11,948 Jan-15-2020, 07:29 PM
Last Post: mckinneycm
  Manipulating Excel with Python. Spacely 2 3,681 Jun-25-2019, 01:57 AM
Last Post: Dequanharrison
  Manipulating CSV Prince_Bhatia 1 1,982 Apr-25-2019, 11:55 AM
Last Post: Gribouillis
  Manipulating an Excel Workbook Stanimal 4 3,036 Jan-18-2019, 11:03 PM
Last Post: Stanimal
  Manipulating Binary Data arsenal88 10 8,747 Apr-25-2017, 02:30 PM
Last Post: snippsat
  Manipulating files Python 2.7 hugobaur 6 8,527 Nov-01-2016, 12:28 PM
Last Post: hugobaur

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020