I'm currently working on a program that I have been assigned that is just getting the word and line count of a sonnet. The first bit of code here works and is the proper output my professor is looking for, even though it includes the first 2 lines of the sonnet.
import string
def main():
ifName = input("What file would you like to analyze? ")
ofName = input("What file should the results be written to? ")
infile = open(ifName, "r")
outfile = open(ofName, "w")
lineCount = 0
wordCount = 0
for line in infile:
lineCount +=1
wordLine = line.split()
L = len(wordLine)
wordCount += L
print("The file", ifName, "had:", file= outfile)
print("words =", wordCount, file= outfile)
print("lines =", lineCount, file= outfile)
print("The results have been printed to:", outfile)
infile.close
outfile.close
main()
However, the next part of the assignment is to get the same results using a second function, "countNum" with the parameter of "line". So countNum(line).
Here is the code I have been messing around with to see if I can get it to work.
import string
def countNum(line):
wordCount = 0
wordLine = line.split()
L = len(wordLine)
wordCount +=L
print(wordCount)
def main():
ifName = input("What file would you like to analyze? ")
ofName = input("What file should the results be written to? ")
infile = open(ifName, "r")
outfile = open(ofName, "w")
lineCount = 0
wordCount = 0
for line in infile:
lineCount +=1
wordTotal += countNum(line)
##wordLine = line.split()
##L = len(wordLine)
##wordCount += L
## print("The file", ifName, "had:", file= outfile)
## print("words =", wordCount, file= outfile)
## print("lines =", lineCount, file= outfile)
## print("The results have been printed to:", outfile)
infile.close
outfile.close
main()
If you were wondering, this is the sonnet.txt file:
Shakespeare’s Sonnet 18
Shall I compare thee to a summer's day?
Thou art more lovely and more temperate:
Rough winds do shake the darling buds of May,
And summer's lease hath all too short a date:
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance or nature's changing course untrimm'd;
But thy eternal summer shall not fade
Nor lose possession of that fair thou owest;
Nor shall Death brag thou wander'st in his shade,
When in eternal lines to time thou growest:
So long as men can breathe or eyes can see,
So long lives this, and this gives life to thee.
Your countNum is printing the result instead of returning it:
def countNum(line):
return len(line.split())
Also, your close methods need to have () after them. They aren't actually executing:
infile.close
outfile.close
to
infile.close()
outfile.close()
def count(line):
return len(line.split())
def main():
infile = open(input("File to analyse: "), "r")
file = infile.read().splitlines()
infile.close()
lineCount = len(file)
wordTotal = 0
for line in file:
words = count(line)
wordTotal += words
print("Lines:", lineCount)
print("Words:", wordTotal)
main()
You can use len to get the length of a list. The list of rows is given by f.readlines() and the list of words in a line is given by line.split(). And we can use the builtin sum to sum across a list and be super pythonic
https://docs.python.org/3/library/functions.html#sum
You should use Python's automatic closing by using the with keyword. https://docs.python.org/3/tutorial/inputoutput.html
so we'd have:
with open(infilename, "r") as infile, open(outfilename, "w") as outfile:
lines = infile.readlines()
linecount = len(lines)
wordcount = sum([len(line.split()) for line in lines])
print(linecount)
print(wordcount)
See https://docs.python.org/3.6/tutorial/datastructures.html#list-comprehensions for list comprehensions
Related
I am making a text based game that once finished should write certain variables to a text file:
==============
Story0:
-----------
Players name: name
Players characteristic: characteristic
This works fine, however what I wish to implement is a way for the function this
text comes from to also display which story it is generating meaning that it would write: StoryX on the X'th time that the program has been run.
I am unfamiliar with writing to and reading from python files and I'm afraid I am very far off what would be the best solution. The following code represents what I have so far (providing an error):
def Endscript(numberofcycles):
with open("playerstories.txt", 'a') as f:
f.write("================\n")
f.write("Story" + str(numberofcycles) + ": \n")
f.write("-----------\n")
f.write("Players name: " + Player.name)
f.write("\n")
f.write("Players characteristic: " + Player.char)
f.write("\n")
with open("number.txt", "r") as f:
numberofcycles = f.readline()
numberofcycles = int(numberofcycles)
numberofcycles += 1
with open("number.txt", "w") as f:
numberofcycles = str(numberofcycles)
f.write(numberofcycles)
What I have attempted to do here is to change the value of a number, (yes just one number) from within the number.txt file in accordance with the changing X value.
Thank you for any help you can provide
Is this what you mean?
def Endscript():
with open("playerstories.txt", 'a') as f:
with open("number.txt", "r") as file:
numberofcycles = file.readline()
numberofcycles = int(numberofcycles)
f.write("================\n")
f.write("Story" + str(numberofcycles) + ": \n")
f.write("-----------\n")
f.write("Players name: " + Player.name)
f.write("\n")
f.write("Players characteristic: " + Player.char)
f.write("\n")
with open("number.txt", "w") as f:
numberofcycles += 1
numberofcycles = str(numberofcycles)
f.write(numberofcycles)
The error is caused by the undefined numberofcycles, if so it is because you haven't read into the txt file to retrieve the numberofcycles.
You have the right idea, just remember that each run your game will be a completely independent process so your Endscript() function really does not know the numberofcycles unless you have already read that from disk. Here is quick function to access a single file and increment the value each time, returning that value for your use.
import os
def increment_counter(file_name):
if not os.path.exists(file_name):
with open(file_name, 'w') as fp:
fp.write("1")
return 1
else:
with open(file_name, 'r') as fp:
current_count = int(fp.read().strip())
with open(file_name, 'w') as fp:
fp.write(str(current_count + 1))
return current_count + 1
fname = 'test_counter.txt'
print(increment_counter(fname))
print(increment_counter(fname))
print(increment_counter(fname))
print(increment_counter(fname))
Output:
1
2
3
4
You can couple this with a reset_counter() which just deletes the file to reset it
Hi i want to create a code that read a text file and print the average of each person in the file, i have this in the folder
boys.txt
ed lin,3,1,4,2
thomas block,6,3,3
Functions.py
def read_txt(file_txt):
file = open(file_txt, "r")
lines = file.readlines()
file.close()
return lines
from Functions import read_txt
file_txt = input("input file: ")
read_txt(file_txt)
lines = (sum(lines)/len(lines))
print(lines)
so i want to print something like
ed lin: 2.5
thomas block: 4
but i dont know how to continue and with my code i get error
Try changing your other file to:
from Functions import read_txt
file_txt = input("input file: ")
lines read_txt(file_txt)
lines = '\n'.join(['%s: %s' % (line.split(',')[0], sum(list(map(int, line.strip().split(',')[1:]))) / len(line.strip().split(',')[1:])) for line in lines])
print(lines)
Output:
ed lin: 2.5
thomas block: 4
I don't know why you are not allowed to use format, but without using it, it would be a little too tedious... In the meantime, here is another way you can do.
with open("boys.txt", "r") as f:
contents = f.read()
lines = contents.strip().split("\n")
for l in lines:
boy = l.split(",")
length = len(boy)
score = 0
for i in range(1, length):
score += int(boy[i])
average = score / (length-1)
print("{}: {}".format(boy[0], average))
You could also use
from Functions import read_txt
file_txt = input("input file: ")
with open(file_txt, 'r') as f:
lines = f.readlines()
for line in lines:
name = line[0: line.index(',')]
data = [int(num) for num in (line[line.index(',')+1:].split(','))]
nums = len(data)
data = sum(data)
print(f"{name}: {data/nums}")
So I want to write a function annotate() which takes a file name as a parameter and prints it to a new file out_annotated.txt with:
the original text
row number
the total amount of words up to and including that row.
Let's say my .txt file is as following:
hello you
the sun is warm
I like dogs
I want the output to be:
hello you 1 2
the sun is warm 2 6
I like dogs 3 9
The code I used before was
def main():
length = count_rows("file.txt")
print(length)
def count_rows(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
if __name__ == "__main__":
main()
But how do I progress into making a new .txt file with the output including row numbers and the total amount of words?
You can create the name of your output file using os.path:
base, ext = os.path.splitext(fname)
out_path = base + "_annotated" + ext
Now you can open them both: one for reading one for writing, while holding a total words counter. Using enumerate as you did is good to keep track of line numbers, but according to your example you want to start from 1. We will split the lines to count words:
total_words = 0
with open(fname) as f_in, open(out_path, 'w') as f_out:
for line_num, line in enumerate(f_in, start=1):
total_words += len(line.split())
Lastly, because you want to add at the end of each line, you need to avoid the ending '\n', so you can write the lines after you strip them and add the row number and word count:
f_out.write("{} {} {}\n".format(line.strip(), line_num, total_words))
All together we have:
import os
def count_rows(fname):
base, ext = os.path.splitext(fname)
out_path = base + "_annotated" + ext
total_words = 0
with open(fname) as f_in, open(out_path, 'w') as f_out:
for line_num, line in enumerate(f_in, start=1):
total_words += len(line.split())
f_out.write("{} {} {}\n".format(line.strip(), line_num, total_words))
Running this on a file named file.txt with the contents as your example, produces a file called file_annotated.txt with contents of:
hello you 1 2
the sun is warm 2 6
I like dogs 3 9
Something like this will work:
def AppendNumbers(input_file, output_file):
# initialize variables:
total_number_of_words = 0
with open(input_file, 'r') as in_file, open(output_file, 'w+') as out_file:
for line in in_file.readlines():
# get number of words on each line:
number_of_words_per_line = len(line.split(' '))
# add to total word count:
total_number_of_words += number_of_words_per_line
# add words to new line:
new_line = line.replace('\n', '')
new_line = new_line + ' ' + str(number_of_words_per_line) + ' ' + str(total_number_of_words) + '\n'
# write new line to outfile:
out_file.write(new_line)
if __name__ == "__main__":
input_file = 'file.txt'
output_file = 'out_file.txt'
AppendNumbers(input_file, output_file)
I am new to programming and I need help finding how many times the user input occurs inside of a txt file. The code I currently have is:
myfile = open("WorldSeriesWinners.txt")
count = 0
team = input("Enter in the team name that won the world series: ")
line = myfile.readline()
myfile.readline()
while team in line:
count += 1
myfile.readline()
print("The", team, "won the world series", count, "times")
myfile.close()
The output I get from this is:
Enter in the team name that won the world series: New York Yankees
The New York Yankees won the world series 0 times
How would I get it to show many times a specific team won? Thanks in advance.
team = input('Enter team name: ')
count = 0
with open("WorldSeriesWinners.txt") as f:
for line in f:
if team in line:
count += 1
print('{} won the world series {} times'.format(team, count)
Go through line by line and check each line using if statements
Try the following:
import re
def world_series_count(team):
with open("WorldSeriesWinners.txt") as f:
data = f.read()
items = re.findall(team, data)
return len(items)
team = input('Enter a team name: ')
count = world_series_count(team)
print('{} won the world series {} times'.format(team, count))
Why are you people complicating matters way over necessary.
This will count occurrences of text in a txt file given by path, regardless of text formatting (unless it is hyphenized):
def count_in_file (path, text):
f = open(path, "rb")
c = f.read()
f.close()
return " ".join(c.split()).count(text.strip())
A little tweak will be needed to support unicode txt files. But there. Simple and easy.
If the txt file is extremely big, then perform this using buffering with static chunk size:
def count_in_file (path, text, chunksize=4096):
text = text.strip()
f = open(path, "rb")
f.seek(0, 2)
fsize = f.tell()
f.seek(0)
if fsize<=chunksize:
c = f.read()
f.close()
return " ".join(c.split()).count(text)
count = 0
l = len(text)
c = f.read(chunksize)
r = c[-l:]
while c:
count += " ".join(c.split()).count(text)
if r!=text: f.seek(f.tell()-l+1)
c = f.read(chunksize)
r = c[-l:]
f.close()
return count
Well, this now is a bit complicated. But if a file is really, really big, and it is formatted over lines, this is a good way to do it.
i have this piece of code:
asm = open(infile)
asmw = open(outfile, "w")
shutil.copyfile(infile, outfile)
for x in range(0, 8):
xorreg.append("xor " + reg[x] + ", " + reg[x])
for line in asm:
if any(s in line for s in xorreg):
found += line.count(xorreg[x])
print line
i want to write some text lines in the file right before "line" (the one printed)
how can i do that?
Thanks
This script appends to every lien containing the string Gandalf a new string The greatest wizard of all times was:
# show what's in the file
with open("some_file.txt", 'r') as f:
print f.read()
new_content = []
with open("some_file.txt", "r") as asmr:
for line in asmr.readlines():
if "Gandalf" in line:
# we have a match,we want something but we before that...
new_content += "The greatest wizard of all times was:"
new_content += line
# write the file with the new content
with open("some_file.txt", "w") as asmw:
asmw.writelines(new_content)
# show what's in the file now
with open("some_file.txt", 'r') as f:
print f.read()