newline

System new line separator in Ruby

筅森魡賤 提交于 2021-01-20 04:20:08
问题 How can I determine new line separator used by OS (LF, CR/LF or other), in Ruby? 回答1: Not sure if there is a direct solution to get the type of newline based on OS, but there is the $/ variable that holds the "input record separator". By default this will be "\n". (Documentation here) You can detect the OS and then set $/ to the "correct" value. To detect OS: puts RUBY_PLATFORM # => 'i386-linux' require 'rbconfig' puts Config::CONFIG['target_cpu'] # => 'i386' puts Config::CONFIG['target_os']

Trailing newlines and the bash 'read' builtin

和自甴很熟 提交于 2021-01-18 06:35:26
问题 In bash, this works: echo -n $'a\nb\nc\n' | while read x; do echo = $x =; done The while loops through three times = a = = b = = c = But imagine a text file that doesn't have the conventional trailing newline. I think that read should still work for all three lines, but it doesn't. I just get: echo -n $'a\nb\nc' | while read x; do echo = $x =; done = a = = b = The help read in bash doesn't really clarify. Note: I don't need this resolved, and I can see some ways to fix it myself. I am curious

Trailing newlines and the bash 'read' builtin

痴心易碎 提交于 2021-01-18 06:35:05
问题 In bash, this works: echo -n $'a\nb\nc\n' | while read x; do echo = $x =; done The while loops through three times = a = = b = = c = But imagine a text file that doesn't have the conventional trailing newline. I think that read should still work for all three lines, but it doesn't. I just get: echo -n $'a\nb\nc' | while read x; do echo = $x =; done = a = = b = The help read in bash doesn't really clarify. Note: I don't need this resolved, and I can see some ways to fix it myself. I am curious

Best way of removing single newlines but keeping multiple newlines

我们两清 提交于 2021-01-04 10:46:12
问题 What would be the most pythonic way of removing single newlines but keeping multiple newlines from a string? As in "foo\n\nbar\none\n\rtwo\rthree\n\n\nhello" turning into "foo\n\nbar one two three\n\n\nhello" I was thinking about using splitlines(), then replacing empty lines by "\n" and then concatenating everything back again, but I suspect there is a better/simpler way. Maybe using regexes? 回答1: >>> re.sub('(?<![\r\n])(\r?\n|\n?\r)(?![\r\n])', ' ', s) 'foo\n\nbar one two three\n\n\nhello'

Best way of removing single newlines but keeping multiple newlines

落花浮王杯 提交于 2021-01-04 10:44:45
问题 What would be the most pythonic way of removing single newlines but keeping multiple newlines from a string? As in "foo\n\nbar\none\n\rtwo\rthree\n\n\nhello" turning into "foo\n\nbar one two three\n\n\nhello" I was thinking about using splitlines(), then replacing empty lines by "\n" and then concatenating everything back again, but I suspect there is a better/simpler way. Maybe using regexes? 回答1: >>> re.sub('(?<![\r\n])(\r?\n|\n?\r)(?![\r\n])', ' ', s) 'foo\n\nbar one two three\n\n\nhello'

Best way of removing single newlines but keeping multiple newlines

守給你的承諾、 提交于 2021-01-04 10:43:07
问题 What would be the most pythonic way of removing single newlines but keeping multiple newlines from a string? As in "foo\n\nbar\none\n\rtwo\rthree\n\n\nhello" turning into "foo\n\nbar one two three\n\n\nhello" I was thinking about using splitlines(), then replacing empty lines by "\n" and then concatenating everything back again, but I suspect there is a better/simpler way. Maybe using regexes? 回答1: >>> re.sub('(?<![\r\n])(\r?\n|\n?\r)(?![\r\n])', ' ', s) 'foo\n\nbar one two three\n\n\nhello'

Best way of removing single newlines but keeping multiple newlines

别说谁变了你拦得住时间么 提交于 2021-01-04 10:42:08
问题 What would be the most pythonic way of removing single newlines but keeping multiple newlines from a string? As in "foo\n\nbar\none\n\rtwo\rthree\n\n\nhello" turning into "foo\n\nbar one two three\n\n\nhello" I was thinking about using splitlines(), then replacing empty lines by "\n" and then concatenating everything back again, but I suspect there is a better/simpler way. Maybe using regexes? 回答1: >>> re.sub('(?<![\r\n])(\r?\n|\n?\r)(?![\r\n])', ' ', s) 'foo\n\nbar one two three\n\n\nhello'

Keep newline character and selectively indent in string during gsub

允我心安 提交于 2020-12-15 04:59:06
问题 Original title: Keep newline character in string during gsub There is a post, where I try to convert JSON to markdown unordered lists. It is almost done, but there is a pattern which I can not handle. If a string has a space, newline, space sequence in it, then it will be treated as the list item hyphen. If I try to avoid this using some reference to a newline character, then nothing works as I expect. Input JSON: https://gist.github.com/hermanp/381eaf9f2bf5f2b9cdf22f5295e73eb5 Preferred

C# Console - How to ReadLine() without a new line? ( or undo a \n or a vertical \b)

最后都变了- 提交于 2020-12-11 04:22:37
问题 I want to know if it is possible to get back to the last line when I'm at the beginning of the next line? (in C# Console, of course) I mean Console.WriteLine() cause going to the next line and I want to stay in my line even after pressing enter. (And I think there isn't another way to ReadLine without going to the next line , is there?) I found that Console.SetCursorPosition() can be useful, like below: int x, y; Console.WriteLine("Please enter the point's coordinates in this form (x,y):");

C# Console - How to ReadLine() without a new line? ( or undo a \n or a vertical \b)

懵懂的女人 提交于 2020-12-11 04:22:30
问题 I want to know if it is possible to get back to the last line when I'm at the beginning of the next line? (in C# Console, of course) I mean Console.WriteLine() cause going to the next line and I want to stay in my line even after pressing enter. (And I think there isn't another way to ReadLine without going to the next line , is there?) I found that Console.SetCursorPosition() can be useful, like below: int x, y; Console.WriteLine("Please enter the point's coordinates in this form (x,y):");