I want to remove a particular column from a file, basically it should be passed as null using Python (not using dataframe)

I have a .dat file which ahs description column that contains text with commas,semi-colon, double quotes. The inner double quotes is causing a problem while loading the data to temp table. So, I just want to eliminate the column from the file or not load any data from that column.

I am looking for a python program that can help me remove inner double quotes (using replace function) or using pop/del to remove that column from that file.

See my code below, which has issues removing the column

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import os
import csv
import sys
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, quoting=csv.QUOTE_ALL, quotechar=qualifierOUT, delimiter=delimiterOUT)
try:
[row.pop(10) for row in reader:]
index = 0
for field in row:
row[index] = str(row[index].replace(delimiterOUT, "")) # delimiter
row[index] = str(row[index].replace(qualifierOUT, "")) # qualifier
row[index] = str(row[index].replace(';', ""))
row[index] = str(row[index].replace('"', '')) # unix newline
#row[index] = str(row[index].replace('rn', "")) # windows newline
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)
except Exception as e:
print(str(e))
sys.exit(45)
sys.exit(0)
</code>
<code>import os import csv import sys 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, quoting=csv.QUOTE_ALL, quotechar=qualifierOUT, delimiter=delimiterOUT) try: [row.pop(10) for row in reader:] index = 0 for field in row: row[index] = str(row[index].replace(delimiterOUT, "")) # delimiter row[index] = str(row[index].replace(qualifierOUT, "")) # qualifier row[index] = str(row[index].replace(';', "")) row[index] = str(row[index].replace('"', '')) # unix newline #row[index] = str(row[index].replace('rn', "")) # windows newline 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) except Exception as e: print(str(e)) sys.exit(45) sys.exit(0) </code>
import os
import csv
import sys

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, quoting=csv.QUOTE_ALL, quotechar=qualifierOUT, delimiter=delimiterOUT)

            try:
               [row.pop(10) for row in reader:]

                    index = 0
                    for field in row:
                        row[index] = str(row[index].replace(delimiterOUT, ""))   # delimiter
                        row[index] = str(row[index].replace(qualifierOUT, ""))   # qualifier
                        row[index] = str(row[index].replace(';', ""))             
                        row[index] = str(row[index].replace('"', ''))            # unix newline
                        #row[index] = str(row[index].replace('rn', ""))        # windows newline
                        
                        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)
  except Exception as e:
    print(str(e))
    sys.exit(45)

sys.exit(0)

Data sample from the file:

Header:
invoice number,invoice date,vendor number,vendor site ID,supplier site CODE,invoice description,invoice currency code,invoice total amount,line number,line amount,line description,account code,business unit,business center,department,issue code,project,task number

Input:
315,1992-02-14,2501,641,PHIL,PRINT INT’ T,USD,117,75,71,”PAN – RETRIEVE AND REVIEW REPORT FOR “LIFE LOGO” IN CLASS (2029- 17) RE: REGISTR FEB 8, 2024; REPORT TO CLIENT AND REQUEST INSTRUCTIONS TO PROCEED; UPDATE RECORDS AND ACTION IN DATA.”,6400,5951,51820,1172,08,,

Output after running the above program:

“315”,”1992-02-14″,”2501″,”641″,”PHIL”,”PRINT INT’ T”,”USD”,”117″,”75″,”71″,”PAN – RETRIEVE AND REVIEW REPORT FOR “LIFE LOGO” IN CLASS (2029- 17) RE: REGISTR FEB 8″,”2024; REPORT TO CLIENT AND REQUEST INSTRUCTIONS TO PROCEED; UPDATE RECORDS AND ACTION IN DATA.”””,”6400″,”5951″,”51820″,”1172″,”08″,””,””

issue:
The “2024; REPORT TO CLIENT AND REQUEST INSTRUCTIONS TO PROCEED; UPDATE RECORDS AND ACTION IN DATA.” is shifted to next column in the database table.
So, I just want to remove this entire line description column from the file.

Please help this is very urgent
Thanks.

So, I just want to remove this entire line description column from the file. and just replace null or ” in the place of line description for all the rows in the file

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật