Home > Mobile >  remove comma from csv row list
remove comma from csv row list

Time:09-17

My csv file row - enter image description here

My code-

with open("G:/Downloads/whatever - Sheet1 (1).csv", 'r') as read_obj:
    csv_reader = reader(read_obj)
    header = next(csv_reader)
    # Check file as empty
    if header != None:
        # Iterate over each row after the header in the csv
        for row in csv_reader:
            # row variable is a list that represents a row in csv
            row = row.replace(",", "")
            docs = list(nlp.pipe(row))

I am getting the error AttributeError: 'list' object has no attribute 'replace'. How do I solve it? Mainly I want to remove all the comma from csv file.

CodePudding user response:

If my understanding is correct, you want to get the output of your csv without the commas, here your row variable doesn't contain commas but instead contains the data in a list as in:

[item1, item2, item3]

if you want to convert that into a string, you would want to do something like:

output = ''.join(row)

to replace your

row = row.replace(",","")

If I misunderstood your question, please let me know.

CodePudding user response:

The row is a list of column values for that particular line. You don't need to replace the commas, instead read the values in the list and concatenate to create a single string and pass it to nlp.

with open("G:/Downloads/whatever - Sheet1 (1).csv", 'r') as read_obj:
    csv_reader = csv.reader(read_obj)
    header = next(csv_reader)
    # Check file as empty
    if header != None:
        # Iterate over each row after the header in the csv
        for row in csv_reader:
            # row variable is a list that represents a row in csv
            # concatenate the string to a single string
            concatenated_value = ''.join(row)
            
            docs = list(nlp.pipe(concatenated_value))
  • Related