Reputation: 59
I Would like to remove range lines: 15 - 405061, but i would like to have structure of my CSV Database file. My Script (below) is fully working, but after executing this script, my structure of file is damaged.
Database before removing line:
After removing using python script:
The Example CSV File:
"Rec No","Guid","Recordnumber","Site","Cardnumber","Empnumber","Date","Time","Device","Direction","Type","Status","Deleted","User","Accessdata","Jobdata","Originaldate","Originaltime","Originaldirection","Company","Branch","Department","Costcenter","Category","Scale","Emptype","Workgroup","Job","Quantity","Jobdata1","Clo:comment"
13,"Here}WR@tdLKTYDf",0,"Here","2012 ","2012 ",75261,2932601,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
16,"Here}WR@[^VKdpB[",0,"Here","6224 ","6224 ",75261,2973401,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
17,"Here}WR@LLZKsWzJ",0,"Here","6074 ","6074 ",75261,2988601,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
18,"Here}WR@Do^Kc~DC",0,"Here","6241 ","6241 ",75261,3008001,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
20,"Here}WR@tzdKHAbS",0,"Here","6107 ","6107 ",75261,3033001,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
28,"Here}WR@OSsK}]ID",0,"Here","6011 ","6011 ",75261,3092201,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
30,"Here}WR@NZwKHFJU",0,"Here","7000 ","7000 ",75261,3109201,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
34,"Here}WR@yp|KzRFh",0,"Here","2014 ","2014 ",75261,3130001,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
39,"Here}WR@y\GLDT]v",0,"Here","9998 ","~Invalid ",75261,3175601,"A "," "," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
40,"Here}WR@UeKLEKFp",0,"Here","6229 ","6229 ",75261,3191001,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
56,"Here}WR@][oLKJXg",0,"Here","6229 ","6229 ",75261,3339201,"A ","O"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
57,"Here}WR@ePpLXhOS",0,"Here","6229 ","6229 ",75261,3343201,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
61,"Here}WR@Gs{LQbhq",0,"Here","7017 ","7017 ",75261,3388201,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
62,"Here}WR@lv}LObFx",0,"Here","1067 ","1067 ",75261,3396801,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
73,"Here}WR@{[mMzwPx",0,"Here","7018 ","7018 ",75261,3591001,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
74,"Here}WR@bvmMKH~h",0,"Here","9998 ","~Invalid ",75261,3593601,"A "," "," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
81,"Here}WR@[gFNbCR`",0,"Here","9998 ","~Invalid ",75261,3695601,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
92,"Here}WR@JPnNVM|o",0,"Here","9998 ","~Invalid ",75261,3858201,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
97,"Here}WR@c|xNw~uy",0,"Here","1306 ","1306 ",75261,3897601,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
98,"Here}WR@MWyNYTfQ",0,"Here","9998 ","~Invalid ",75261,3899601,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
103,"Here}WR@hcAOqZx|",0,"Here","1050 ","1050 ",75261,3928601,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
114,"Here}WR@hk|PxNDF",0,"Here","6011 ","6011 ",75261,4441401,"A ","O"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
115,"Here}WR@}tNSdWws",0,"Here","6229 ","6229 ",75261,5041201,"A ","O"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
123,"Here}WR@vw~SSPsg",0,"Here","2014 ","2014 ",75261,5236601,"A ","O"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
124,"Here}WR@_RSnah",0,"Here","6241 ","6241 ",75261,5238201,"A ","O"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
141,"Here}WR@fMITokJC",0,"Here","6074 ","6074 ",75261,5280401,"A ","O"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
142,"Here}WR@oBJT|FtK",0,"Here","1067 ","1067 ",75261,5283201,"A ","O"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
145,"Here}WR@n`LTnfnJ",0,"Here","9998 ","~Invalid ",75261,5292801,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
147,"Here}WR@yQRTx||x",0,"Here","1449 ","1449 ",75261,5314601,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
167,"Here}WR@\soUZ`R",0,"Here","9998 ","~Invalid ",75261,5697401,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
170,"Here}WR@dp@VE{z",0,"Here","1059 ","1059 ",75261,5768201,"A ","I"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
171,"Here}WR@LKAVdyDi",0,"Here","1059 ","1059 ",75261,5771401,"A ","O"," ","A"," "," ",0,0,0,0," ",0,0,0,0,0,0,0,0," ",0," ",""
My Python Script Code:
import csv
filename = rb'./Converted Database/DB_After.csv'
start_skip_row = 15
stop_skip_row = 405061
def readCSVFile(file_name: str, start: int, stop: int) -> list:
with open(file_name, 'r') as file_content:
data_after_deletion = [line.replace('\n', '') for idx, line in enumerate(file_content) if
not start - 1 <= idx < stop]
print(data_after_deletion)
return data_after_deletion
def writeCSVFile(data: list) -> None:
with open('./Converted Database/DB_Compiled.KingAdamI', 'w', newline='') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(data)
data_after_deletion = readCSVFile(filename, start_skip_row, stop_skip_row)
writeCSVFile(data_after_deletion)
Do you have any ideas why after execution of this script, my structure of CSV has ben destroyed? Thank you
Upvotes: 0
Views: 40
Reputation: 2313
csvwriter acts as Dictwriter and expects the parameter passed to writerows() function to be an iterable of strings or numbers and a dictionary mapping fieldnames to strings or numbers.
In above code, since you are passing a list of strings, it has just iterated through every character and delimited with comma.
Changing this statement under readCSVFile()
data_after_deletion = [line.replace('\n', '') for idx, line in enumerate(file_content) if
not start - 1 <= idx < stop]
as below
data_after_deletion = [{line.replace('\n', '')} for idx, line in enumerate(file_content) if
not start - 1 <= idx < stop]
May help to retain the structure.
Upvotes: 0
Reputation: 71707
No need to use csv.reader
or csv.writer
. You can simply read the input csv file line by line and write those lines which are not in the range [15, 405061]
to output csv file:
with open('./Converted Database/DB_After.csv') as f1,\
open('./Converted Database/DB_Compiled.KingAdamI', 'w') as f2:
for idx, line in enumerate(f1, 1):
if idx < 15 or idx > 405061:
f2.write(line)
Upvotes: 1