![]() |
How to comma seprate csv but not all cells - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to comma seprate csv but not all cells (/thread-12424.html) |
How to comma seprate csv but not all cells - Prince_Bhatia - Aug-24-2018 I am reading a csv file whose data structures looks like this [attachment=464] i am trying to read it and convert it into comma separated list so that i can access every element separately below is my code: def Residential(): global url, parse_url filename = "Residential.csv" f = open(filename, "r") for line in f.readlines(): data = line.split(",") builderid = data[0] print(builderid) buildername = data[1] print(buildername) buildingname = data[2] print(buildername) address = data[3] city = data[4] locality = data[5] building_id = data[6] apartment = data[7].replace("\"","") print(type(apartment)) status = data[8].replace("\"","") print(status) cdate = data[9] bhk = data[10] rnumber = data[11] rurl = data[12] print(data) Residential()element 7 and 10 which are property type and bhk also got separated by "," how can i not to separate these by comma and read element 7 into list and element 10 as it is. Like this i want to read it ['11916', 'testBuild', 'Test Building', 'Udaipur sector-5 Hiran Magri', '181', '753180', '794950', [1,2], 'UC', '2018-08-22', "2BHK, 3BHK", '', '\n']this is result i have received: ['11916', 'testBuild', 'Test Building', 'Udaipur sector-5 Hiran Magri', '181', '753180', '794950', '"1', '2"', 'UC', '2018-08-22', '"2BHK', ' 3BHK"', '', '\n'] RE: How to comma seprate csv but not all cells - buran - Aug-24-2018 try to use csv module, don't split at commas on line 6. I guess the csv file is properly formated, then property type columns is double quoted because there is comma in the value and csv module will process it correctly |