简体   繁体   English

将[和]之间的每个单词的首字母大写在文本文件中

[英]Capitalize first letter of each word between [ and ] in text file

EDIT: This question is different from other "capitalize first letter" questions because it requires capitalization only between "[" and "]". 编辑:这个问题不同于其他“首字母大写”问题,因为它只需要在“[”和“]”之间进行大写。 Since the title was incomplete, I have edited it. 由于标题不完整,我编辑了它。

I have a text file in which I need to reformat the text. 我有一个文本文件,我需要重新格式化文本。

I have tried to loop lines and words while the file is open in 'r+', but have been unsuccessful. 我试图在'r +'中打开文件时循环行和单词,但是没有成功。

Here is a sample: 这是一个示例:

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 the [ ] to be capitalized. 我希望[]之间的每个单词中的第一个字母大写。 And as a bonus I'd love spaces between [ ] to be replaced with underscores. 作为奖励,我喜欢[]之间的空格被下划线取代。

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 of just word.capitalize . 我也尝试过f.write(word.capitalize())而不仅仅是word.capitalize All results were equally tragic. 所有结果都同样悲惨。

The way I would code this : 我编写代码的方式:

  1. load the whole content of your file 加载文件的全部内容
  2. use the module re ( re.sub would help) to transform parts that need to be 使用模块rere.sub会帮助)转换需要的部分
  3. overwrite the file with the transformed text 用转换后的文本覆盖文件

The implementation : 实施:

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

You can try using the .title() method asked here in a similar question . 您可以尝试在类似的问题中使用此处.title()方法。 Also, make sure that you write the changes back to the file with f.write() . 此外,请确保使用f.write()回文件。 Just having the mode as r+ doesn't persist anything to the file for you. 将模式设为r +并不会为您保留任何文件。

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

Here is my solution to your problem, it uses regex to handle the actual replacements, but this could easily be completed by writing your own parser. 这是我的问题解决方案,它使用regex来处理实际的替换,但这可以通过编写自己的解析器轻松完成。

Using this as my test inp 用这个作为我的测试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)
)
'''

The process is then simply to simply format each match the regex expression makes. 然后,该过程只是简单地格式化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)

And without the use of regex : 并且不使用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 = ""

You can open present file somescript.sql' in read mode. 您可以在读取模式下打开当前文件somescript.sql' Read each line and process it eg if there is a column name then capitalized first latter and replace space by _ This can be done using regular expression. 读取每一行并处理它,例如,如果有一个列名,则首先大写后一个并用_替换空格。这可以使用正则表达式完成。 Latter you can delete old file and rename temp file as old-filed name. 您可以删除旧文件并将临时文件重命名为旧文件名。

script.py: 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

I did as follows, I have two files: 我做了如下,我有两个文件:

answer$ ls
script.py  somescript.sql

somescript file is: 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)
)

To explain: o.write(l.replace(c, c.title().replace(' ', '_'))) 解释: o.write(l.replace(c, c.title().replace(' ', '_')))

  1. o.write(x) write a string x in file o.write(x)在文件中写一个字符串x
  2. l.replace(c, c.title().replace(' ', '_')) replace first argument c that is a column name by second argument c.title().replace(' ', '_') , Second argument is c titled followed by space is replaced by _ . l.replace(c, c.title().replace(' ', '_'))用第二个参数c.title().replace(' ', '_')替换第一个参数c ,它是一个列名c.title().replace(' ', '_') ,第二个参数是c标题,后面的空格被_替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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