i want to make a program that count the letter A in the texts
f = open('text.txt', 'r')
content = f.read()
i = 1
for each a in content:
i = i + 1``
print(i)
The best way to do this particular task is by using the built-in count() function. Then the code becomes:
f = open('text.txt', 'r')
content = f.read()
print(content.count("A"))
I think this is what you want:
with open('text.txt', 'r') as f:
i = 0
for char in f:
if char == 'a':
i += 1
print(f'There are {i} a\'s in the txt file.')
string.count(substring)
content.count("a")
Related
I want that if a word already exit in Dico.txt it doesn't write it again.
e = input("Mots: ")
f = e.split(" ")
with open("Dico.txt", "a") as f2:
for line in f:
for word in line.split():
f2.write(word + '\n')
You can use a list for this:
e = input("Mots: ")
f = e.split(" ")
u = []
with open("Dico.txt", "a") as f2:
for line in f:
for word in line.split():
if not word in u:
f2.write(word + '\n')
u.append(word)
else:
continue
#!/usr/bin/env python
f = open('Dico.txt', 'r')
f2 = open('new.txt', 'w')
w = input('The word? ')
for line in f:
for word in line.split():
l = line.replace(w, "")
f2.write(l.strip(" "))
So I was supposed to find the number of occurences of a text file, by following this kind of format.
But my code probably is entirely wrong.
text = open('text3.txt','r')
d = dict()
fname = input('Enter the file name:')
l = input('Enter letter to be searched:')
k = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
for i in words:
for letter in i:
if(letter == 1):
k = k+1
print('Occurences of the letter:')
print(k)
Can someone help me for this?
yeah you can iterate the characters instead:
fname = input('Enter the file name:')
char = input('Enter letter to be searched:')
i = 0
with open(fname, 'r') as f:
data = f.read()
for each in data:
if each == char:
i += 1
print(i)
I am trying to print the line that is after text matched in the text file.
Something like this:
import re
afterlines=3
with open(filename, 'r') as f:
for line in f:
if line.strip()== ls_losses:
row = f.readline(+afterlines)
print (row)
print ("true")
I would just use a temporary counter.
import re
afterlines=3
temporary_lines = ''
with open(filename, 'r') as f:
for line in f:
if line.strip() == ls_losses:
counter = afterlines
if counter > 0:
temporary_lines += f.readline()
counter -= 1
else:
print(temporary_lines)
temporary_lines = '' # Reinitialize to get ready for the next match
print ("true")
so I just want to do some simple math addition
get number from the first file ( let's say it's 1)
and number from the second file ( let's say it's 2)
so what I'm getting is 12, not 3
I would really appreciate the help.
myfile = open('file.txt', "r")
onecaracter = myfile.read(2)
with open('liczba1.txt', 'w') as f:
print(onecaracter, file=f)
myfile = open('file.txt', "r")
twocaracter = myfile.read(myfile.seek(4))
with open('liczba22.txt', 'w') as f:
print(twocaracter, file=f)
with open('liczba1.txt', "r") as file:
z = file.read(1)
with open('liczba22.txt', "r") as fil:
b = fil.read(1)
print(z + b)
The variables z and b are likely str types, and the + operator is defined on str types as concatenation. You can cast the two variables as integers and they should add as you expect, i.e.:
print(int(z) + int(b))
To illustrate this, you can always print out the type of a variable:
print(type(z))
myfile = open('file.txt', "r")
onecaracter = myfile.read(2)
with open('liczba1.txt', 'w') as f:
print(onecaracter, file=f)
myfile = open('file.txt', "r")
twocaracter = myfile.read(myfile.seek(4))
with open('liczba22.txt', 'w') as f:
print(twocaracter, file=f)
with open('liczba1.txt', "r") as file:
z = file.read(1)
with open('liczba22.txt', "r") as fil:
b = fil.read(1)
print(int(z) + int(b))
you should do a type cast:
total = int(z) + int(b)
print(total)
When reading and printing through my files, printing through my cousole gives me the correct result, but writing to the outfile does not
with infile as f :
lines = f.readlines()
new_line = " "
for line in lines:
new_line = ''.join(line).replace('*',letter.upper())
new_line = new_line.replace(':',letter.lower())
print(new_line)
This prints out all of the letters that I inputted
with infile as f :
lines = f.readlines()
new_line = " "
for line in lines:
new_line = ''.join(line).replace('*',letter.upper())
new_line = new_line.replace(':',letter.lower())
outfile.write(new_line)
It only gives me the last letter of the word inputted.
folder = r"C:\Users\sarah\Documents\a CPS 111\Bonus PA\stars\stars"
# os.listdir(folder) returns a list of files in folder
file_list = os.listdir(folder)
letter_art = {}
word = str(input("Please input a letter: "))
word = word.upper()
for fname in file_list:
letter_extension_list = fname.split(".")
for letter in word:
key = letter
value = letter_extension_list[1]
value = "%s."%(key) + value
letter_art[key] = value
fname = "\\".join([folder, value])
infile = open(fname, "r")
outfile = open("word_art.txt", "w")
with infile as f :
lines = f.readlines()
new_line = " "
for line in lines:
new_line = ''.join(line).replace('*',letter.upper())
new_line = new_line.replace(':',letter.lower())
print(new_line)
outfile.write(new_line)
infile.close()
outfile.close()
This is the code I am currently working with. I am taking in symbols from a txt file and changing them to the coornading letter depending on what the user inputed
Open the output file before the loop instead of within it:
outfile = open("word_art.txt", "w")
for letter in word:
with open("test.txt",'r') as f :
lines = f.readlines()
with open('out.txt','w') as outfile:
for line in lines:
new_line = line.replace('*',letter.upper())
new_line = new_line.replace(':',letter.lower())
outfile.write(new_line)
This worked for me.
EDIT:
TigerhawkT3 is correct. I checked out your full code and you were opening the file again and again inside the loop, each time discarding the prior changes.