line

Remove lines that contain certain string

Remove lines that contain certain string Question: I’m trying to read a text from a text file, read lines, delete lines that contain specific string (in this case ‘bad’ and ‘naughty’). The code I wrote goes like this: infile = file(‘./oldfile.txt’) newopen = open(‘./newfile.txt’, ‘w’) for line in infile : if ‘bad’ in line: line …

Total answers: 10

How to draw a line in Python Mayavi?

How to draw a line in Python Mayavi? Question: How to draw a line in 3D space in Python Mayavi? Is there a function from MLAB module that allows me to specify the start and end point of the line which will be drawn? Asked By: Luka || Source Answers: Check the documentation for mayavi; …

Total answers: 3

Search and get a line in Python

Search and get a line in Python Question: Is there a way to search, from a string, a line containing another string and retrieve the entire line? For example: string = qwertyuiop asdfghjkl zxcvbnm token qwerty asdfghjklñ retrieve_line(“token”) = “token qwerty” Asked By: Ben || Source Answers: With regular expressions import re s=””” qwertyuiop asdfghjkl …

Total answers: 4

How to read specific lines from a file (by line number)?

How to read specific lines from a file (by line number)? Question: I’m using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this? Asked By: eozzy || Source Answers: File objects have a .readlines() method which …

Total answers: 30

Delete final line in file with python

Delete final line in file with python Question: How can one delete the very last line of a file with python? Input File example: hello world foo bar Output File example: hello world foo I’ve created the following code to find the number of lines in the file – but I do not know how …

Total answers: 10