Question

In: Computer Science

original code: filename = "CCL-mbox-tiny.txt" with open(filename, 'r') as f: for line in f.readlines(): print(line.strip()) 1....

original code:

filename = "CCL-mbox-tiny.txt"
with open(filename, 'r') as f:
for line in f.readlines():
print(line.strip())
1. Now modify the code above to print the number of lines in the file, instead of printing the lines themselves. You'll need to increment a variable each time through the loop and then print it out afterwards.

There should be **332** lines.

2. Next, we'll focus on only getting lines addressing `X-DSPAM-Confidence:`. We do this by including an `if` statement inside the `for` loop. This is a very common pattern in computing used to search through massive amoutns of data.

You need to edit line 4 of the code below to only print lines which begin with `X-DSPAM-Confidence:` You know you got it working if in your output there are only **5** rows.

filename = "CCL-mbox-tiny.txt"
with open(filename, 'r') as f:
for line in f.readlines():
if ?:
print(line.strip())

3. The final step is to figure out how to parse out the confidence value from the string. For example for the given line: X-DSPAM-Confidence: 0.8475 we need to get the value 0.8475 as a float.

The strategy here is to replace X-DSPAM-Confidence: with an empty string, then calling the float() function to convert the results to a float.

Now Try It

Write code to parse the value 0.8475 from the text string 'X-DSPAM-Confidence: 0.8475'.

line = 'X-DSPAM-Confidence: 0.8475'
number = #TODO remove 'X-DSPAM-Confidence:' , then convert to a float.
print (number)

Python Language

Solutions

Expert Solution

1.

#variable for storing number of lines
count =0
#assigning file name in a variable
filename = "CCL-mbox-tiny.txt"
#opening file name
with open(filename, 'r') as f:
    #reading line by line
    for line in f.readlines():
        #incrementing number of lines
        count = count+1
#prints count of lines
print(count)

2.

#variable for storing number of lines
count =0
#assigning file name in a variable
filename = "CCL-mbox-tiny.txt"
#opening file name
with open(filename, 'r') as f:
    # reading line by line
    for line in f.readlines():
        #checks wether line starts with "X-DSPAM-Confidence:"
        if line.split(" ")[0] =="X-DSPAM-Confidence:":
            #incrementing number of lines
            count = count+1
#prints count of lines
print(count)

3.

#assigning string to a variable
line = "X-DSPAM-Confidence: 0.8475"
#extracts value and converts it to float
number = float(line.split(" ")[1])
#prints number
print(number)
#prints type of number
print(type(number))

i hope the answer is clear and satisfactory

if you have any doubts feel free to ask in comment section

please give me thumbs up


Related Solutions

def compareRisk(compareCountry, countryList, filename):     filename = open(filename, "r")     content = filename.readlines()     returnList =...
def compareRisk(compareCountry, countryList, filename):     filename = open(filename, "r")     content = filename.readlines()     returnList = []     for row in content [1:]:         line = row.split(",")         name = line[0]         population = float(line[1])         infectedPopulation = float(line[2])     for compareCountry in content:         comparePopulation = float (line[1])         compareInfected = float (line[2])     for name in countryList:         if population < comparePopulation:             if infectedPopulation > compareInfected:                 returnList.append(name)             else:                 return "No countries"     return returnList...
CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row...
CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row = next(data_from_file) for index,column_header in enumerate(header_row): print(index,column_header) How many COUMNS (not rows!) will be printed in the above code?
f : [a, b] → R is continuous and in the open interval (a,b) differentiable. f...
f : [a, b] → R is continuous and in the open interval (a,b) differentiable. f rises strictly monotonously ⇒ ∀x ∈ (a, b) : f ′(x) > 0. (TRUE or FALSE?) f rises strictly monotonously ⇐ ∀x ∈ (a, b) : f ′(x) > 0. (TRUE or FALSE?) f is constant ⇐⇒ ∀x∈(a,b): f′(x)=0 (TRUE or FALSE?) If f is reversable, f has no critical point. (TRUE or FALSE?) If a is a “minimizer” of f, then f ′(a)...
I need original java code that completes this program and gets it to print out results...
I need original java code that completes this program and gets it to print out results just like in the example. Also please upload answer in a word document format only. Implement both linear search and binary search, and see which one performs better given an array 1,000 randomly generated whole numbers (between 0-999), a number picked to search that array at random, and conducting these tests 20 times. Each time the search is conducted the number of checks (IE...
C++ Code Using all for loops 1. Print 5 lines with 1 asterisk per line 2....
C++ Code Using all for loops 1. Print 5 lines with 1 asterisk per line 2. Print 5 asterisk on 1 line. Endl at the end of the line. 3. Ask for a positive # and print that many asterik on the next line. 4. Using 2 for loops one inside of another that only prints 1 asterik, print a hill shaped triangle: Ex( Input a number a: 4 * ** *** **** 5. Change the for statements to print...
5. (a) Let f : R \ {−1} → R, f(x) = x+1. Show that f...
5. (a) Let f : R \ {−1} → R, f(x) = x+1. Show that f is injective, but not surjective. (b) Suppose g : R\{−1} → R\{a} is a function such that g(x) = x−1, where a ∈ R. Determine x+1 a, show that g is bijective and determine its inverse function.
What are the errors in this code? //1. //Filename: CarbonFootprintTest.java //The file tests the Car class...
What are the errors in this code? //1. //Filename: CarbonFootprintTest.java //The file tests the Car class and CarbonFootprint class public class CarbonFootprintTest {     public static void main (String [] args) {    CarbonFootprint[] obj = new CarbonFootprint[2];    obj[0] = new CarbonFootprint(20);    obj[1] = new Car(30);     System.out.println("Carbon Foot Print for each item (lbs):\n");     //additional info for to give general idea of program     for (CarbonFootprint test: obj)        test.getCarbonFootprint(); }//end main method }//end class //2. Filename: CarbonFootprint.java...
1) Show that if A is an open set in R and k ∈ R \...
1) Show that if A is an open set in R and k ∈ R \ {0}, then the set kA = {ka | a ∈ A} is open.
Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
1. Basic use of R/R Studio. Solve the following problem in R and print out the...
1. Basic use of R/R Studio. Solve the following problem in R and print out the commands and outputs. (a) Create a vector of the positive odd integers less than 100; Remove the values greater than 60 and less than 80; Find the variance of the remaining set of values (b) What’s the difference in output between the commands 2*1:5 and (2*1):5? Why is there a difference? (c) If you wanted to enter the odd numbers from 1 to 19...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT