简体   繁体   中英

To remove strings in between quotes and inserting a new line before a special character from a text file and saving it

I am trying to read a text file and remove anything in between quotes ("") and also any kind of commas and semi-colons.

I also want to insert a line before the string of characters 'qwe'.

I have no experience with python and I have been tasked to this in python.

f = open('filename.txt', 'r')
for line in f:
    x=''
    for i in range(len(line)):
        if str(line[i]) != ',':
            x += str(line[i])
        if str(line[i]) == ',':
            pass
print (x)

Here's some example code to read and remove comma's from a text file, you can start from this and try adding all the other stuff you want to do. Just have this code saved in the same place as the text file and type the text file's name where it says filename.

import re
with open('code.txt', 'r') as infile, open('noquotes.txt', 'w') as outfile:
    for line in infile:
        x=''
        line=re.sub('".+?"', '', line)
        for i in range(len(line)):
            if str(line[i])!=',':
                x+=str(line[i])
            if str(line[i])== ',':
                pass
    outfile.write(x)
    outfile.close(x)

I got it :D

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