Python Forum
Correct errors in text file.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Correct errors in text file.
#1
I trying to write a program, which will correct errors in text file, for example TOm--> Tom, EUrope-->Europe, but if word has two letters, for example IT, program will ask the user if he wants to change second letter.I must use re.sub.
I don't have idea. I have:
import re
def correct(to_file):
    with open(to_file,'r') as f:
        for line in f:
            for word in line.spilit():
But, How to take into account the conditions of the task in re.sub..
Reply
#2
What have words with two letters in common? len(two_letter_word) == 2
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
I have now:
def correct(to_file):
    with open(to_file, 'r') as f:
        text = f.read()

    for word in text.split(" "):
        if len(word) == 2:
            print(word)
            x = int(input("DO you want to change second letter? (YES=1/NO=0):"))
            if x == 1:
                list2 = re.sub('([aA-zZ][A-Z]+ )','what here', text)

        else:
            list1 = re.sub('([A-Z][A-Z][a-z]+)',what here, text)



    print(list1)
    print(list2)
But, I don't have idea what to write here: "what here". I want to change only second letter.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert text from an image to a text file Evil_Patrick 5 4,315 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  reading text file and writing to an output file precedded by line numbers kannan 7 10,470 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Writing incorrect passwords to a file till its correct garth 2 4,973 Feb-10-2017, 11:41 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020