How to Read File Line by Line Python

If you have a CSV file, you may want to read csv file instead. In case you have a text-based file and want to to read lines from that file using Python, let check following approaches: 1. Iterate over file object File objects are lazy iterators, so just iterate over it after openning it. filename = 'filename.txt' with open(filename) as f: for line in f: print(line) Alternatively, if you have multiple files, use fileinput. [Read more]

How to iterate over files in directory python

This tutorial will show you some ways to iterate files in a given directory and do some actions on them using Python. 1. Using os.listdir() This method returns a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries ‘.’ and ‘..’ even if they are present in the directory. Example: print out all paths to files that have jpg or png extension in C:\Users\admin directory [Read more]

Trim A Binary Search Tree

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree. Trim A Binary Search Tree Solution /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class TrimABinarySearchTree { public TreeNode trimBST(TreeNode root, int L, int R) { if(root == null) { return root; } if(root. [Read more]

Meeting Rooms II

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. For example, Given [[0, 30],[5, 10],[15, 20]], return 2. Meeting Rooms Ii Solution /** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Interval(int s, int e) { start = s; end = e; } * } */ public class MeetingRoomsII { public int minMeetingRooms(Interval[] intervals) { int[] starts = new int[intervals. [Read more]

Meeting Rooms

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example, Given [[0, 30],[5, 10],[15, 20]], return false. Meeting Rooms Solution /** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Interval(int s, int e) { start = s; end = e; } * } */ public class MeetingRooms { public boolean canAttendMeetings(Interval[] intervals) { if(intervals == null) { return false; } // Sort the intervals by start time Arrays. [Read more]

Using PySnooper for Python Debugging

When your Python code doesn’t run as expected or when you want to examine if your program is running correctly, you may use a full-fledged debugger with breakpoints and watches. But in some cases, you can’t be bothered to set one up right now. You want to know which lines are running and which aren’t, and what the values of the local variables are. Most people would use print lines, in strategic locations, some of them showing the values of variables. [Read more]

Direct Download Links Docker

Docker requires user login in order to download Docker for Windows and Docker for Mac. This is so annoying for those who just need docker to run images.

Fortunately, the direct download links are still valid and you can download docker for windows and Mac without login.

Here is the links:


1 Introduction to Atomic Atomic classes Chemically, we know that atoms are the smallest unit of a general substance and are inseparable in chemical reactions. In our case Atomic means that an operation is uninterruptible. Even when multiple threads are executed together, once an operation starts, it will not be disturbed by other threads. Therefore, the so-called atomic class is simply a class with atomic/atomic operational characteristics. The atomic classes of the concurrent package java. [Read more]

How to Extract Text From Pdf in Python 3

To extract text from pdf using Python 3, we will use the pdfminer.six package. Install pdfminer.six pip install pdfminer.six Extract text from pdf import io from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter from pdfminer.converter import TextConverter from pdfminer.layout import LAParams from pdfminer.pdfpage import PDFPage def convert_pdf_to_txt(path): '''Convert pdf content from a file path to text :path the file path ''' rsrcmgr = PDFResourceManager() codec = 'utf-8' laparams = LAParams() with io.StringIO() as retstr: with TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams) as device: with open(path, 'rb') as fp: interpreter = PDFPageInterpreter(rsrcmgr, device) password = "" maxpages = 0 caching = True pagenos = set() for page in PDFPage. [Read more]

How to remove all white space in Python

If you want to remove leading and ending spaces, use str.strip(): sentence = ' newbie dev' sentence.strip() >>> 'newbie dev' Remove ALL spaces in a string, even between words: Use str.replace(): sentence = ' newbie dev' sentence.replace(" ", "") >>> 'newbiedev' Use regex import re sentence = ' newbie dev' sentence = re.sub(r"\s+", "", sentence, flags=re.UNICODE) Remove ONLY DUPLICATE spaces: Use regex sentence = ' hello apple' " ". [Read more]