1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Removal of commas from a csv file column using Python [closed]

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I have a CSV file received from the vendor, the file has around 20 columns and there is one column line_description. At times, the line description values are having inner double quotes and the description value is enclosed with double quotes. Also, in few files the line description has commas, which is making the part of line description to move to next columns in cs file and when the file is loaded to database table, the part of line description is shifted to next fields due to commas within the value. The business has confirmed that we don't need to load the description field to table.

    So, I need


    1. the description value to be removed from the csv file

      or


    2. remove commas from description field

      or


    3. shift the part of the description values from next columns back to description column in csv file.

    Can we remove those inner commas.

    The line description value is shifted to account_code and rest of the columns values are shifted too.

    [​IMG]

    I have a code written in python to replace the double quotes, other special characters and to make description value null, which is working fine, but the inner commas is creating a problem which makes the rest of the data to shift to next columns,

    import os
    import csv
    import sys
    import re

    from shutil import copyfile

    sourcedir=sys.argv[1]
    sourcefile=sys.argv[2]
    targetdir=sys.argv[3]
    targetfile=sys.argv[4]
    qualifierIN=sys.argv[5]
    delimiterIN=sys.argv[6]
    qualifierOUT=sys.argv[7]
    delimiterOUT=sys.argv[8]

    curDir = os.getcwd()
    sep = os.path.sep

    if os.path.exists(sourcedir):
    print("Source Directory : {x}".format(x=sourcedir))
    os.chdir(sourcedir)
    if os.path.isfile(sourcefile):
    print("Source File : {x}".format(x=sourcefile))
    else:
    print("Source File Not Found : ", sourcefile)
    else:
    print("Source Directory Not Found : ",sourcedir)
    sys.exit(5)



    if os.path.exists(targetdir):
    print ("Target Directory : {x}".format(x=targetdir))
    else:
    print ("Target Directory Not Found :",targetdir)
    os.makedirs(targetdir)

    try:
    with open(sourcefile, 'rt') as fin:
    reader = csv.reader(fin, delimiter=delimiterIN, quotechar=qualifierIN )
    os.chdir(targetdir)
    with open(targetfile, 'wt') as fout:
    writer = csv.writer(fout, quotechar=qualifierOUT, delimiter=delimiterOUT)

    try:
    for row in reader:

    index = 0
    for field in row:
    print ("r[9]:",row[9])
    row[index] = str(row[index].replace(delimiterOUT, "")) # delimiter
    row[index] = str(row[index].replace(qualifierOUT, "")) # qualifier
    row[index] = str(row[index].replace(';', "")) # Pipe will be used as a delimiter later, so remove it now
    row[index] = str(row[index].replace('"', '')) # unix newline
    #row[index] = str(row[index].replace('\r\n', "")) # windows newlin
    row[9]=""



    index += 1


    print(row)

    writer.writerow(row)
    except csv.Error as e:
    print("Error: ", str(e))
    sys.exit(25)
    finally:
    fout.close()
    fin.close()
    os.chdir(curDir)
    except Exception as e:
    print("Error : ", str(e))
    sys.exit(35)



    try:
    os.chdir(sourcedir)
    os.remove(sourcefile)
    copyfile(targetfile, sourcefile)
    #os.rename(targetfile, sourcefile)
    os.remove(targetfile)

    except Exception as e:
    print(str(e))
    sys.exit(45)

    sys.exit(0)

    Continue reading...

Compartilhe esta Página