An alternative approach would be to write it as a generator comprehension:
def send(self, queue, fd):
non_blank_lines = (line.strip() for line in fd
if line.strip())
for line in non_blank_lines:
queue.write(json.loads(data))
This approach can be beneficial (terser) if you are applying a function that can take an iterator: e.g. python3 print
non_blank_lines = (line.strip() for line in fd
if line.strip())
print( * non_blank_lines, file = 'foo')
To do away with the multiple calls to strip(), chain together generator comprehensions
stripped_lines = (line.strip() for line in fd) non_blank_lines = (line for line in stripped_lines if line)
If the file won't contain lines such as
\ n
but instead only \n
, then this way will be noticeably faster:
def send(self, queue, fd):
for line in fd:
if line != '\n':
queue.write(json.loads(line.strip()))
Timeit values:
using: strip()::1.8722578811916337
using: line != '\n'::1.0126976271093881
using: line != '\n'
and line != ' \n'::1.2862439244170275
Describe the bug PSR-2 includes requirement: "There MUST NOT be trailing whitespace at the end of non-blank lines.". But in PSR-12 requirement changed to: "There MUST NOT be trailing whitespace at the end of lines.". Any line. Not only blank ones. PHPCS seems to still use PSR-2 requirement, even if call it with PSR-12 standard.,Expected behavior In output summary, there should be another error indicated in the line 4 with the same (similar?) message: Whitespace found at end of line,It doesn't reference a reason for the change, besides the wording being confusing. That leads me to believe that the author believed they were keeping the intended message from PSR-2, but making the wording clearer. I have no idea if that's correct, but the commit does say "Trailing whitespace is not allowed in any situation", so I think that is clear enough to ban it on lines that only contain whitespace.,Additional info What is worth mentioning, using Squiz standard there's no such behaviour and line 4 is marked as "Whitespace found at end of line", so it's somehow possible. Tested on the same file with only changing standard to Squiz.
< ? php
// some irrelevant code
echo "There are only two spaces in previous line";
//
echo "There are two slashes and two spaces in previous line";
… y whitespace
All of the above solutions will also remove lines that contain nothing but whitespace. If you only want to remove empty lines, and leave lines that contain spaces or tabs etc, change the /\S/ to /^$/ in all of the above examples. ,Sometimes the other commands will fail to remove blank lines if the lines contain a blank space. The following will remove all blank lines including lines that contain only blank spaces., 1 @fedorqui indeed, but I think you meant grep '.' file, we want to remove empty lines, not keep them. – terdon Jun 12, 2015 at 19:26 ,I have a file (hosts) with some lines without content, how do I remove that lines without content?
Type the following command:
sed '/^$/d'
input.txt > output.txt
Use sed
and edit the file in place.
sudo sed - i - rn '/\S/p' / etc / hosts
Same, but with Perl:
sudo perl - i - ne 'print if /\S/' / etc / hosts
Newer versions of GNU awk
, in place again:
sudo awk - i inplace '/\S/'
file
You can use Vim in Ex mode:
ex - sc v / . / d - cx hosts
Sometimes the other commands will fail to remove blank lines if the lines contain a blank space. The following will remove all blank lines including lines that contain only blank spaces.
grep - v '^[[:space:]]*$'
input.txt > output.txt
Starting from the first blank line from Normal mode, press d/.. Press Enter.,d will delete until / finds the first non-blank character (anything matching .).,I want to delete all the blank lines between the two lines of text., Can a posterior probability be larger than 1 when more samples are available?
I have some text like this:
Line 1 of text Line 2 of text.
Another solution is to join consecutive lines found between non-blank lines. ,In that case, the following will delete all empty lines, or lines that contain only combinations of space, tab or IDEOGRAPHIC SPACE. ,In CJK languages (Chinese, Japanese, Korean), the Unicode character IDEOGRAPHIC SPACE may be used. If you have 'encoding' utf-8 (and do not have the 'l' flag in 'cpoptions'), you can use \u3000 in a search pattern to specify the UTF-16 hex code for IDEOGRAPHIC SPACE. :help /\] ,Power of g Some advanced techniques to condense multiple blank lines
Use either of the following commands to delete all empty lines:
: g / ^ $ / d: v / . / d
If you want to delete all lines that are empty or that contain only whitespace characters (spaces, tabs), use either of:
: g / ^ \s * $ / d: v / \S / d
You may want to condense multiple blank lines into a single blank line. The following will delete all trailing whitespace from each line, then replace three or more consecutive line endings with two line endings (a single blank line):
: % s / \s\ + $ //e
: % s / \n\ {
3,
}
/\r\r/e
In that case, the following will delete all empty lines, or lines that contain only combinations of space, tab or IDEOGRAPHIC SPACE.
: g / ^ [\t\u3000] * $ / d
An alternative procedure, which should work in other encodings, would be to enter the CJK space directly into the pattern. That is, you would type the following, but instead of '#' you would enter a CJK space.
: g / ^ [\t #] * $ / d
By default sed prints all processed input (except input that has been modified/deleted by commands such as d). Use -n to suppress output, and the p command to print specific lines. The following command prints only line 45 of the input file: ,If no addresses are given, the command is performed on all lines. The following command replaces the word ‘hello’ with ‘world’ on all lines in the input file: ,If you do not specify INPUTFILE, or if INPUTFILE is -, sed filters the contents of the standard input. The following commands are equivalent: ,The following program is a no-op. The b command (the only command in the program) does not have a label, and thus simply restarts the cycle. On each cycle, the pattern space is printed and the next input line is read:
sed SCRIPT INPUTFILE...
sed 's/hello/world/'
input.txt > output.txt
sed 's/hello/world/'
input.txt > output.txt
sed 's/hello/world/' < input.txt > output.txt
cat input.txt | sed 's/hello/world/' - > output.txt
sed - i 's/hello/world/'
file.txt
sed - n '45p'
file.txt
sed - n '1p ; $p'
one.txt two.txt three.txt