Question

In: Computer Science

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

Solutions

Expert Solution

CODE:

with open("sample.txt", "r") as f:

  lines = f.readlines()

with open("sample.txt", "w") as f:

  for line in lines:

    # write only those files which doesn't have # in beginning

    if len(line)==len(line.strip("#")):

      f.write(line)

INPUT:

OUTPUT:

Please upvote if you like my answer and comment below if you have any queries or need any further explanation.


Related Solutions

Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice). This program behaves similarly to the AddValueToArray program from before. However, instead of modifying the array in-place, it will return a new array holding the modification. The original array does not change. For simplicity, the array used is “hard-coded” in main, though the method you write should work with any array. Example output with the command-line argument 3 is shown below: Original array: 3...
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice)....
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice). This program contains two methods which you will need to write: swapRows: Swaps the contents of two rows, given a two-dimensional array and the indices of the rows to swap. swapCols: Swaps the contents of two columns, given a two-dimensional array and the indices of the columns to swap. main contains some code which will call swapRows and swapCols, for the purpose of informal...
Download the Take.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Take.java file, and open it in jGrasp (or a text editor of your choice). This program will “take” a given number of elements from a given array, using the command-line arguments as an array, and the hard-coded value of 3 elements to take. Example output of this program with the command-line arguments foo bar baz is shown below: foo bar baz In the above example, three elements were taken. However, it's possible that we may request to take...
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice). This program takes a number of command line arguments, converts them to integers, and then determines if they all have the same value. An example run of the program is below, using the command-line arguments 1 2 3: Are equal: false A further example is shown below, with the command-line arguments 2 2: Are equal: true In the event that the program is given...
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice). This program will randomly print out a compass direction, given a seed value for produce a random number with java.util.Random. Compass directions are mapped to integers according to the following table: Compass Direction Integer ID North 0 Northeast 1 East 2 Southeast 3 South 4 Southwest 5 West 6 Northwest 7 Further details are in the comments of Compass.java. import java.util.Random; import java.util.Scanner; public...
Step 1: Edit SumMinMaxArgs.java Download the SumMinMaxArgs.java file, and open it in jGrasp (or a text...
Step 1: Edit SumMinMaxArgs.java Download the SumMinMaxArgs.java file, and open it in jGrasp (or a text editor of your choice). This program takes a number of command line arguments, parses them as ints, and then displays: The arithmetic sum of the arguments The smallest argument The largest argument If you're unsure how to pass command-line arguments to a program with jGrasp, see this tutorial. Example output of this program with the command-line arguments 1 2 3 4 5 is shown...
Step 3: Edit MultiSplit.java Download the MultiSplit.java file, and open it in jGrasp (or a text...
Step 3: Edit MultiSplit.java Download the MultiSplit.java file, and open it in jGrasp (or a text editor of your choice). You will need to write a method that will take an array of Strings (String[]) along with a regular expression, and will call split() on each one of those Strings with the given regular expression. The result of this method should be a two-dimensional array of String (String[][]). You will also need to write a method to print out this...
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT