Python Forum
Read text file, process data and print specific output - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Read text file, process data and print specific output (/thread-24567.html)



Read text file, process data and print specific output - Happythankyoumoreplease - Feb-20-2020

Please someone HELP!!!!
I have the below homework and no code I've tried worked so far!!!!!
_________________________________________________________________________________________________________
Write a Python script that will read a text file whose lines contain numbers separated by white-space; as output, the program will produce for each line of the original text file another line that will contain two numbers: the first will be the total number of entries in the line, and the second will be the sum of the numbers in the line.
Example:
Input file (test1.txt) contents:
1 3 5 12 0
2 3
1
-1 1
Expected Output:
Enter text file name: test1.txt
Output:
5 21
2 5
1 1
2 0


RE: Help!!!! Simple Code - micseydel - Feb-20-2020

What have you tried? (Post it in code tags.)


RE: Read text file, process data and print specific output - buran - Feb-20-2020

And in addition to micseydel's advice - use meaningful and descriptive thread titles. I edited yours this time.


RE: Read text file, process data and print specific output - jefsummers - Feb-20-2020

Use .split() to split on the space (look it up). Convert to a list of numbers. Use len(the list) for your count. Loop and add to get the sum. Do this for each line in the file.
You are having trouble getting started but the loop can be done through a "list comprehension". Once you have solved it look that up to see how your loop could be condensed to a single line.