【问题标题】:Python remove range lines from csv inc structure of databasePython从数据库的csv inc结构中删除范围线
【发布时间】:2023-04-02 19:45:02
【问题描述】:

我想删除范围行:15 - 405061,但我想拥有我的 CSV 数据库文件的结构。我的脚本(如下)可以正常工作,但执行此脚本后,我的文件结构已损坏。

删除行前的数据库:

使用python脚本删除后:

CSV 文件示例:

"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,"          ",""

我的 Python 脚本代码:

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)

您知道为什么执行此脚本后,我的 CSV 结构被破坏了吗?
谢谢

【问题讨论】:

    标签:
    python
    database
    line