Felix
Felix

Reputation: 53

Python: Capitalise specific words in a text file

I am trying to write a python script to format SQL codes for better readability.

eg. converting specific lowercase words to uppercase: select -> SELECT

I am trying to do this using the reading and writing function in Python. However, I am stuck. Here is my code:

words = ['select', 'from', 'where']
w = open('03_TextUpper.txt', 'w')

with open('03_TextLower.txt', 'r') as file:

    for line in file:
        for word in line.split():
            if word in words:
                w.write( word.upper() )

    w.write( line ) 

This prints out the uppercase of the specific words, but does not remove the lowercase words.

Is there a better way of writing this in Python?

Upvotes: 2

Views: 1156

Answers (4)

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24133

Third party library sqlparse will do this:

>>> sql = 'select * from foo where id in (select id from bar);'
>>> print sqlparse.format(sql, reindent=True, keyword_case='upper')
SELECT *
FROM foo
WHERE id IN
  (SELECT id
   FROM bar);

Upvotes: 0

Martin Evans
Martin Evans

Reputation: 46759

I suggest you do this is two stages:

words = ['select', 'from', 'where']

with open('03_TextUpper.txt') as f_input:
    text = f_input.read()

with open('03_TextUpper.txt', 'w') as f_output:
    for word in words:
        text = text.replace(word, word.upper())

    f_output.write(text)

First read the whole file into memory and make the necessary changes. Next open the file for writing and write it back.

It uses a Python string replace() to replace each word with its uppercase alternative. It does this for the whole file in one go without needing to split the file up.


This can be improved with the use of a regular expression to spot word boundaries:

import re

words = ['select', 'from', 'where']
uppercase = lambda x: x.group(1).upper()
re_replace = re.compile(r'\b({})\b'.format('|'.join(words)))

with open('03_TextUpper.txt') as f_input:
    text = f_input.read()

with open('03_TextUpper.txt', 'w') as f_output:
    f_output.write(re_replace.sub(uppercase, text))

The regular expression is able to carry out all of the replacements in a single call.

Upvotes: 1

Ajax1234
Ajax1234

Reputation: 71451

You can iterate over the file once, and then write the generated list to the output file:

words = ['select', 'from', 'where']
data = [i.strip('\n').split() for i in open('03_TextLower.txt')]
new_words = [[b for b in i if b.lower() in words] for i in data]

with open('03_TextUpper.txt', 'w') as f:
   for line in new_words:
       for word in line:
          f.write("{}\n".format(word))

Upvotes: 0

Larry
Larry

Reputation: 652

You're opening a read and write in the same instance. Try closing your read function and opening a new write function when it's done, and it could solve the problem.

Upvotes: 0

Related Questions