简体   繁体   中英

Editing text file in python

I have a text file that looks like:

CREATE TABLE address(

    location_pk integer NULL,

    City varchar(50) NULL, 

    Address1 varchar(50) NULL, 

    Address2 varchar(50) NULL, 

CREATE TABLE name(

    idinteger NULL,

    City varchar(50) NULL, 

    RootDCTKey integer NULL, 

    Class varchar(50) NULL, 

CREATE TABLE location(

    location_pk integer NULL,

    City varchar(50) NULL, 

    Rootkey integer NULL, 

I need to add a ");" and delete the comma',' after each end of table do that it look like :

CREATE TABLE address(

    location_pk integer NULL,

    City varchar(50) NULL, 

    Address1 varchar(50) NULL, 

    Address2 varchar(50) NULL ); 

CREATE TABLE name(

    idinteger NULL,

    City varchar(50) NULL, 

    RootDCTKey integer NULL, 

    Class varchar(50) NULL );

CREATE TABLE location(

    location_pk integer NULL,

    City varchar(50) NULL, 

    Rootkey integer NULL );

My approach is to write the corrected version of the file to another file

opFile1 = open('txtfile.txt','rb').readlines()
finalfile = open('finaltxtfile.txt','wb')

for i,line in enumerate(opFile1):

    if not line.startswith('CREATE TABLE'):
        finalfile.write(line)
    else:
        #overwrite the last written line with the corrected line
        finalfile.write(opFile1[i-2].rstrip(',')+' );')

        #write the original 'CREATE TABLE' line
        finalfile.write(line)

This approach just adds the corrected line before the CREATE TABLE line and do not remove the original wrong line. Please help with your ideas. Thanks :)

Hope this helps. For each line,with the help of enumerated index, check if the next-to-the next line starts with CREATE (I have made it CREATE, you can modify the string as your need). If yes, perform the replacement line.replace(',',');') and write the line. We also need to replace for the last line; This is added as a condition in the if as or i+1==len(opFile1) . The i+2<len(opFile1) is there so that the index is prevented from going out of bounds.

opFile1 = open('txtfile.txt','r').readlines()
finalfile = open('finaltxtfile.txt','w')

for i,line in enumerate(opFile1):
    if i+2<len(opFile1) and opFile1[i+2].startswith('CREATE') or i+1==len(opFile1):
        finalfile.write(line.replace(',',');'))
    else:
        finalfile.write(line)
finalfile.close()

Btw, you were using binary modes rb , wb to write the string which is unnecessary, I have changed it to normal modes r and w .

If the file isn't huge I wouldn't bother processing each line. I would just read the entire file into a variable, do a find/replace on the last comma, and then write the variable to a new file (or the original file).

您可以使用re来完成工作:

finalfile.write(re.sub(b'NULL,(\s*\n*)(CREATE|\Z)', rb'NULL);\1\2', opFile1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM