Python Forum
How to comma seprate csv but not all cells
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to comma seprate csv but not all cells
#1
I am reading a csv file whose data structures looks like this    


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']
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,125 Aug-19-2022, 08:07 PM
Last Post: Winfried
  How to format Excel column with comma? dee 0 1,375 Jun-13-2022, 10:11 PM
Last Post: dee
  Openpyxl-change value of cells in column based on value that currently occupies cells phillipaj1391 5 9,865 Mar-30-2022, 11:05 PM
Last Post: Pedroski55
  decimal comma DPaul 9 2,296 Feb-22-2022, 12:25 PM
Last Post: DeaD_EyE
  Adding a comma in the resulting value stsxbel 6 2,656 May-22-2021, 09:24 PM
Last Post: stsxbel
  How can I iterate through all cells in a column (with merge cells) with openpyxl? aquerci 1 7,532 Feb-11-2021, 09:31 PM
Last Post: nilamo
  How to instantly add quotation marks and comma for parameters? cheers100 4 8,103 Oct-22-2020, 12:51 PM
Last Post: cheers100
  print scripts example includes comma that seems to serve no purpose flour_power_33 5 2,817 Sep-02-2020, 03:32 AM
Last Post: flour_power_33
  Copy certain cells into new workbook certain cells Kristenl2784 4 2,503 Jul-14-2020, 07:59 PM
Last Post: Kristenl2784
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 4,065 Apr-10-2020, 02:39 PM
Last Post: buran

Forum Jump:

User Panel Messages

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