1

I am trying to lowercase the first letter after a separator or split (":"). This means that if I have a file with lines like this:

hello:world
iamlearning:python
is:cool

I would like to convert it to this:

hello:World
iamlearning:Python
is:Cool

I looked for information on how to do it, saw information and tried to do some tests, but it did not work for me. I can lower case all words, but not the first letter after a separator. Here the code:

fname = input("Enter file name: ")
f = open(fname)
s = f.read().strip().lower()
f.close()
f = open(fname,"w")
f.write(s)
f.close()

If someone can help me, I am trying to make a text editor :)

Thanks in advance.

flaxel
  • 4,173
  • 4
  • 17
  • 30
Drakarys
  • 41
  • 5

2 Answers2

2

have fun making your combo editor :^)

delimiter = ":"

with open("file.txt", "r", encoding='UTF-8')as f:
    data = [f"{x}{delimiter}{y.capitalize()}" for x,y in [tuple(i.strip().split(delimiter)) for i in f.readlines()]] 

with open("file.txt", "w+", encoding='UTF-8')as f: f.write("\n".join(data))

before:

username:password1
email:password2

after:

username:Password1
email:Password2

So whats going on here?

well, ets break down this crazy list comprehension

data = [tuple(i.strip().split(delimiter)) for i in f.readlines()]

which sets data to:

[('username', 'Password1'), ('email', 'Password2')]

basically a list of tuples for each combo in the combolist. ("email", "pass")

So, whats a tuple? think of it as a light ordered list of data that can easily be accessed in loops.

All this does line does is splits the data into 2 parts so i can easily edit the second in my next line. Which brings us to the second loop...

data = [f"{x}{delimiter}{y.capitalize()}" for x,y in data] 

This this goes through our list of tuples data, and just joins them with an f string. you could have just as easily done (x + delimiter + (y.capitalize()))

Obviously you will want to create more modules to use in your tool so you can do the same thing while applying different things. E.g. if you wanted to add an ! to the end of a password you could edit that line and add a function in that applies to y:

import random
addSpecial(y):
    return f"{y}{random.choice(["!", "?", "*", "$"])}

f"{x}{delimiter}{addSpecial(y)}"

some other resources: Tuple unpacking in for loops

Lastly, don't use it yourself to do harm... I know a thing or two because I've seen (or done) a thing or two in that community...

Feel free to contact me via my bio for more information

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
Ironkey
  • 2,568
  • 1
  • 8
  • 30
1

Split the line, capitalize the second part, rebuild the string:

l = 'iamlearning:python'
a, b = l.split(':')
a + ':' + b.capitalize()
#'iamlearning:Python'
DYZ
  • 55,249
  • 10
  • 64
  • 93