An uppercase letter from each word between [and] in a text file

EDIT: This question is different from other questions "make amends for the first letter" because it requires capitalization only between "[" and "]". Since the name was incomplete, I edited it.

I have a text file in which I need to reformat the text.

I tried to loop lines and words while the file was open in 'r +', but was unsuccessful.

Here is an example:

Create Table Data(
    [SOME ID] int,
    [LAST NAME] varchar(30),
    [FIRST NAME] varchar(30),
    [TLA THING] smallint,
    [TLA THING REMARK] varchar(255)
)

I would like the first letter in each word between [] to be capitalized. And as a bonus, I would like the spaces between [] to be replaced by underscores.

The code I tried:

f = open('somescript.sql','r+')
for line in f:
    for word in line:
        word.capitalize()

I also tried f.write(word.capitalize())instead word.capitalize. All results were equally tragic.

+3
source share
4

:

  • re (re.sub ) ,

:

txt = # load your file
pattern = re.compile(r"\[(.*)\]")
transform = lambda mo : mo.group(0).title().replace(" ", "_")
new_txt = pattern.sub(transform, txt)
# write new text
+2

.title(), , . , , f.write(). r + .

f = open('somescript.sql','r+'):
text = f.read()
text = text.title()
f.write(text)
f.close()
+1

, regex , , .

inp

text = '''Create Table Data(
    [lower case id] int,
    [loser case last name] varchar(30),
    [lower case first name] varchar(30),
    [lower case tla thing] smallint,
    [lower case tla thing remark] varchar(255)
)
'''

, regex.

def format_input(val):
    val = val.strip()
    val = val.split()
    new_val = ""
    for word in val:
        new_val += word[0].upper() + word[1:] + "_"
    return new_val[:-1] //Remove the trailing underscore


content = ""
with open('mySQLfile.sql','r') as f:
    for line in f:
        content += line

import re
content = re.sub(r'\[(.*?)\]',lambda m: '['+format_input(m.group(1))+']',content,re.M)

with open('mySQLfile.sql','w') as f:
    f.write(content)

regex:

new_content = ""
buf = ""
in_tag = False
for i in content:
    if in_tag:
        buf += i
    else:
        new_content += i
    if i == '[':
        in_tag = True
    elif i == ']':
        in_tag = False
        new_content += format_input(buf)
        buf = ""
0

somescript.sql' . , . , , _ . .

script.py:

import os, re
with open("somescript.sql") as i: # open sql file for reading 
  with open("temp", "w") as o: # tem file for writing 
    for l in i: # read line by line 
      c = re.match(r".*\[(?P<col_name>.*)\].*", l) # use re to find col_name
      if c: # if column name found  
        c = c.group('col_name') # change col name 
        o.write(l.replace('['+c+']', '['+c.title().replace(' ', '_'))+']')
      else:       #         ^^ col name titled and replace every space by _  
        o.write(l)
os.remove("somescript.sql") # delete old file 
os.rename("temp", "somescript.sql")  # rename file

: :

answer$ ls
script.py  somescript.sql

somescript:

answer$ cat somescript.sql 
Create Table Data(
    [SOME ID] int,
    [LAST NAME] varchar(30),
    [FIRST NAME] varchar(30),
    [TLA THING] smallint,
    [TLA THING REMARK] varchar(255)
)

$ python script.py  # run script 
/answer$ cat somescript.sql 
Create Table Data(
    [Some_Id] int,
    [Last_Name] varchar(30),
    [First_Name] varchar(30),
    [Tla_Thing] smallint,
    [Tla_Thing_Remark] varchar(255)
)

: o.write(l.replace(c, c.title().replace(' ', '_')))

  • o.write(x) x
  • l.replace(c, c.title().replace(' ', '_')) c, c.title().replace(' ', '_'), c, , _.
0

All Articles