AM I DOING THIS RIGHT, PLEASE HELP ME, SO ME MORE EXAMPLES ON HOW TO RIGHT IT :o
C:\home\COP1000\a\3>python test.txt
22 103 699
2 4 22 test.txt
52 223 1683
76 330 2404 total
AM I DOING THIS RIGHT, PLEASE HELP ME, SO ME MORE EXAMPLES ON HOW TO RIGHT IT :o
C:\home\COP1000\a\3>python test.txt
22 103 699
2 4 22 test.txt
52 223 1683
76 330 2404 total
Quick, practical route to a correct "wc" replacement and why the posted suggestions matter.
was right to ask for the code; gave the right intuition but left some edge cases unexplained. A robust approach reads each file line-by-line (so huge files do not get loaded at once), counts physical lines, counts characters from each line, and counts words by matching consecutive non-whitespace tokens (this matches the usual Unix notion that words are separated by whitespace). Be aware: counting newline characters, counting text characters, and counting raw bytes can differ (for example if the final line has no trailing newline or when multibyte encodings are used).
A compact, practical implementation that handles multiple files and prints per-file counts plus a total:
#!/usr/bin/env python3
import sys, re
TOKEN = re.compile(r'\S+')
def counts(filename):
lines = words = chars = 0
with open(filename, 'r', encoding='utf-8', errors='replace') as fh:
for line in fh:
lines += 1
chars += len(line)
words += len(TOKEN.findall(line))
return lines, words, chars
def print_counts(l, w, c, name=None):
if name:
print("{:7d}{:8d}{:8d} {}".format(l, w, c, name))
else:
print("{:7d}{:8d}{:8d}".format(l, w, c))
def main():
files = sys.argv[1:]
totals = [0, 0, 0]
for fn in files:
l, w, c = counts(fn)
totals[0] += l; totals[1] += w; totals[2] += c
print_counts(l, w, c, fn)
if len(files) > 1:
print_counts(*totals, "total")
if __name__ == "__main__":
main() Notes and quick checks: the regex approach handles tabs and multiple spaces correctly; to mimic Unix wc -c (bytes) open files in binary mode and sum len(chunk) instead of text lengths; mismatched counts often come from encoding issues or a missing final newline. Including the exact wc.py source and the command line that produced the sample output will let others reproduce and spot the specific problem.
Jump to Post— vegaseat 1,735You got to show us the code for wc.py. Most of us are not very good in mind reading!
Jump to Post— vegaseat 1,735I got to know what you have been asked to do with your Python program, before I can tell you if you did it right!
You got to show us the code for wc.py. Most of us are not very good in mind reading!
So I Didnt Do This Right
I got to know what you have been asked to do with your Python program, before I can tell you if you did it right!
Word Count. A Common Utility On Unix/linux Systems Is A Small Program Called "wc." This Program Analyzes A File To Determine The Number Of Lines, Words, And Characters Contained Therein. Write A Version Of Wc. The Program Should Accept A File Name As Input And Than Print Three Numbers Showing The Count Of Lines, Words, And Characters In The File.
there is spaces between the numbers
Here is simplified approach, the number of new-line characters '\n' give number of lines, the number of spaces are approximately the number of words, and the number of characters is the length of the text string read from file.
You simply use a for loop and and iterate each character in text counting new-lines, spaces. Don't have Unix and don't know if space counts as a character, also new-line. Can use char_from_text.isalnum() to weed these out.
You can also use, I didn't test, number_of_lines = text.count('\n') function.
A more correct answer for the number of words can be obtained by splitting the text string into list of words with word_list = text.split() and then get length of this list with number_words = len(word_list).
so how would i set this up with what u told me because im a visual learner
C:\home\COP1000\a\3>python test.txt
22 103 699
2 4 22 test.txt
52 223 1683
76 330 2404 total
Here is simplified approach, the number of new-line characters '\n' give number of lines, the number of spaces are approximately the number of words, and the number of characters is the length of the text string read from file.
You simply use a for loop and and iterate each character in text counting new-lines, spaces. Don't have Unix and don't know if space counts as a character, also new-line. Can use char_from_text.isalnum() to weed these out.
You can also use, I didn't test, number_of_lines = text.count('\n') function.
A more correct answer for the number of words can be obtained by splitting the text string into list of words with word_list = text.split() and then get length of this list with number_words = len(word_list).
i having problem using this set up, can u please help me
C:\home\COP1000\a\3>python test.txt
22 103 699
2 4 22 test.txt
52 223 1683
76 330 2404 total
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.