GVKun编程网logo

Writelines写的行没有换行符,只填充文件(document.writeln(为什么没有换行))

26

本文将分享Writelines写的行没有换行符,只填充文件的详细内容,并且还将对document.writeln(为什么没有换行)进行详尽解释,此外,我们还将为大家带来关于.net–Console.W

本文将分享Writelines写的行没有换行符,只填充文件的详细内容,并且还将对document.writeln(为什么没有换行)进行详尽解释,此外,我们还将为大家带来关于.net – Console.WriteLine()和Debug.WriteLine()之间有什么区别?、Bash技巧:把变量赋值为换行符,判断文件是否以换行符结尾、C#中Write()和WriteLine()的区别分析、css inline-block后有换行符变成空白间隔bug的相关知识,希望对你有所帮助。

本文目录一览:

Writelines写的行没有换行符,只填充文件(document.writeln(为什么没有换行))

Writelines写的行没有换行符,只填充文件(document.writeln(为什么没有换行))

我有一个将列表写入文件的程序。该列表是由管道分隔的行的列表,这些行应按如下所示写入文件:

123|GSV|Weather_Mean|hello|joe|43.45122|GEV|temp_Mean|hello|joe|23.45124|GSI|Weather_Mean|hello|Mike|47.45

但是它写了他们这行啊:

123|GSV|Weather_Mean|hello|joe|43.45122|GEV|temp_Mean|hello|joe|23.45124|GSI|Weather_Mean|hello|Mike|47.45

该程序将所有行写成一行,而没有任何换行符。.这让我非常痛苦,我必须弄清楚如何将其反转,但是无论如何,我的程序在哪里出错?我认为写行应该在文件下写行,而不是将所有内容都写到一行。

fr = open(sys.argv[1], ''r'') # source filefw = open(sys.argv[2]+"/masked_"+sys.argv[1], ''w'') # Target Directory Locationfor line in fr:    line = line.strip()    if line == "":        continue    columns = line.strip().split(''|'')    if columns[0].find("@") > 1:        looking_for = columns[0] # this is what we need to search    else:        looking_for = "Dummy@dummy.com"    if looking_for in d:        # by default, iterating over a dictionary will return keys            new_line = d[looking_for]+''|''+''|''.join(columns[1:])            line_list.append(new_line)    else:        new_idx = str(len(d)+1)        d[looking_for] = new_idx        kv = open(sys.argv[3], ''a'')        kv.write(looking_for+" "+new_idx+''\n'')        kv.close()        new_line = d[looking_for]+''|''+''|''.join(columns[1:])        line_list.append(new_line)fw.writelines(line_list)

答案1

小编典典

对于刚接触Python的新手来说,这实际上是一个非常普遍的问题-
尤其是因为在标准库和流行的第三方库中,一些阅读功能会删除换行符,但几乎没有书写功能(与-log有关的东西除外)会添加它们。

因此,有很多Python代码可以执行以下操作:

fw.write(''\n''.join(line_list) + ''\n'')

要么

fw.write(line + ''\n'' for line in line_list)

哪一种是正确的,当然您甚至可以编写自己的writelinesWithNewlines函数来包装它……

但是,只有在无法避免的情况下,才应该这样做。

最好首先创建/保留换行符,如Greg Hewgill的建议:

line_list.append(new_line + "\n")

如esuaro所建议的,例如,可以使用标准库中的csv模块,则可以比文本的原始行更高级地工作,甚至更好。

例如,在定义之后fw,您可以执行以下操作:

cw = csv.writer(fw, delimiter=''|'')

然后,代替此:

new_line = d[looking_for]+''|''+''|''.join(columns[1:])line_list.append(new_line)

你来做这件事:

row_list.append(d[looking_for] + columns[1:])

最后,代替这个:

fw.writelines(line_list)

你来做这件事:

cw.writerows(row_list)

最后,您的设计是“打开文件,然后建立要添加到文件中的行列表,然后一次写入所有行”。如果要打开该文件,为什么不只一行一行地写呢?无论您使用的是简单写入还是csv.writer,它都将使您的生活更简单,并且代码也更易于阅读。(有时出于简单,效率或正确性的原因,一次全部写入一个文件,但是一旦您将open所有内容从移到程序的另一端write,您就几乎失去了所有好处。
-一次)。

.net – Console.WriteLine()和Debug.WriteLine()之间有什么区别?

.net – Console.WriteLine()和Debug.WriteLine()之间有什么区别?

Console.WriteLine()和Debug.WriteLine()之间有什么区别?
Console.WriteLine写入标准输出流,无论是在调试还是释放。 Debug.WriteLine写入 Listeners集合中的跟踪侦听器,但仅当在调试中运行时。当应用程序在发布配置中编译时,Debug元素不会被编译到代码中。

当Debug.WriteLine写入Listeners集合中的所有跟踪监听器时,可能会在多个位置输出(Visual Studio输出窗口,控制台,日志文件,注册监听器的第三方应用程序(我相信DebugView这样做)等)。

Bash技巧:把变量赋值为换行符,判断文件是否以换行符结尾

Bash技巧:把变量赋值为换行符,判断文件是否以换行符结尾

把变量赋值为换行符

在 bash 中,如果要把变量赋值为换行符,写为 ''\n'' 没有效果,需要写为 $''\n''。具体举例如下:

$ newline=''\n''
$ echo $newline
\n
$ newline=$''\n''
$ echo $newline

可以看到,把 newline 变量赋值为 ''\n'',得到的是 \n 这个字符串,而不是换行符自身。

这是 bash 和 C 语言不一样的地方。
在 C 语言中,''\n'' 对应换行符自身,只有一个字符;而 "\n" 对应一个字符串。
但是在 bash 中,''\n'' 也是对应一个字符串。

newline 赋值为 $''\n'',就能获取到换行符自身。查看 man bash 对这个写法的说明如下:

Words of the form $''string'' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:
    \n     new line
    \r     carriage return
    \t     horizontal tab
    \''     single quote
The expanded result is single-quoted, as if the dollar sign had not been present.

即,$''string'' 这个写法可以使用 C 语言的转义字符来获取到对应的字符自身。

判断文件的最后一行是否以换行符结尾

在 Linux 中,可以使用下面命令来判断文件的最后一行是否以换行符结尾:

test -n "$(tail filename -c 1)"

这里使用 tail filename -c 1 命令获取到 filename 文件的最后一个字符。

实际使用时,需要把 filename 换成具体要判断的文件名。

tail 命令可以获取文件末尾的内容。它的 -c 选项指定要获取文件末尾的多少个字节。

查看 man tail 对 -c 选项的说明如下:

-c, --bytes=K

output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file.

即,tail -c 1 命令指定获取所给文件的最后一个字符。

获取到文件的最后一个字符后,要判断该字符是不是换行符。这里不能直接判断该字符是否等于换行符,而是要判断该字符是否为空。

原因在于,使用 $(tail filename -c 1) 命令替换来获取内部命令的输出结果时,bash 会去掉末尾的换行符。

所以当文件的最后一行以换行符结尾时,$(tail filename -c 1) 命令替换会去掉获取到的换行符,最终结果为空,并不会返回换行符自身。

查看 man bash 对命令替换(command substitution)的说明如下:

Command substitution allows the output of a command to replace the command name. There are two forms:
        $(command)
    or
        `command`
Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.

可以看到,经过命令替换后,会去掉末尾的换行符。

由于 $(tail filename -c 1) 命令替换会去掉末尾的换行符,这里使用 test -n 来判断最终结果是否为空字符串。

如果文件最后一行以换行符结尾,那么 $(tail filename -c 1) 的结果为空,test -n 命令会返回 1,也就是 false。

如果文件最后一行没有以换行符结尾,那么 $(tail filename -c 1) 的结果不为空,test -n 命令会返回 0,也就是 true。

可以根据实际需要,改用 test -z 来判断。如果文件最后一行以换行符结尾,$(tail filename -c 1) 的结果为空,test -z 命令会返回 0,也就是 true。

C#中Write()和WriteLine()的区别分析

C#中Write()和WriteLine()的区别分析

Write()和WriteLine()都是System.Console提供的方法,两着主要用来将输出流由指定的输出装置(默认为屏幕)显示出来.
两着间的差异在

Console.WriteLine()方法是将要输出的字符串与换行控制字符一起输出,当次语句执行完毕时,光标会移到目前输出字符串的下一行.
至于Console.Write()方法,光标会停在输出字符串的最后一个字符后,不会移动到下一行。

Write()和WriteLine()区别

  • 都是System.Console提供的方法
  • 都是在屏幕显示
  • Write()显示完毕后不换行,WriteLine()换行

代码示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WriteLine和Write的区别
{
class Program
{
static void Main(string[] args)
{
//WriteLine输出后鼠标显示在下一行开头
//Write输出后不开始一个新行
System.Console.WriteLine("First WriteLine Line");
System.Console.WriteLine("Second WriteLine Line");

System.Console.Write("First Write Line");//First Write Line后面不开始新的一行,直接紧跟着输出Second Write Line
System.Console.Write("Second Write Line");

//passing parameters
System.Console.WriteLine("\nWriteLine:Parameter={0}", 123);
System.Console.Write("Write:Parameter={0}", 456);
System.Console.ReadKey();
}
}
}

输出

First WriteLine Line
Second WriteLine Line
First Write LineSecond Write Line
WriteLine:Parameter=123
Write:Parameter=456

到此这篇关于C#中Write()和WriteLine()的区别分析的文章就介绍到这了,更多相关csharp write与writeline内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

您可能感兴趣的文章:
  • C#判断一个图像是否是透明的GIF图的方法
  • .net c# gif动画如何添加图片水印实现思路及代码
  • C#中使用UDP通信的示例
  • c# 获取照片的经纬度和时间的示例代码
  • 大白话讲解C# 中的委托
  • c# 动态构建LINQ查询表达式
  • 详解c# 协变和逆变
  • c# 播放声音的四种方法
  • C#实现窗体抖动的两种方法
  • c# 制作gif的四种方法

css inline-block后有换行符变成空白间隔bug

css inline-block后有换行符变成空白间隔bug

以前都使用display:block很少使用display:inline-block,近期写样式有时候为了方便我也使用inline-block,不想竟然有个大问题,每个元素之间多了一个空白,而这个空白正是由于换行导致的:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8" />

        <style type="text/css">

            * {

                margin: 0;

                padding: 0;

            }

 

            span {

                margin: 0;

                padding: 0;

                display: inline-block;

                width: 50px;

                height: 20px;

                background: red;

            }

        </style>

    </head>

    <body>

        <span></span><span></span

        <span></span>

        <span></span>

    </body>

</html>

火狐:

IE:

Chrome:

没办法,要想把内联标签表现成块并且不换行只能按照老方法display:block后再float它。

关于Writelines写的行没有换行符,只填充文件document.writeln(为什么没有换行)的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.net – Console.WriteLine()和Debug.WriteLine()之间有什么区别?、Bash技巧:把变量赋值为换行符,判断文件是否以换行符结尾、C#中Write()和WriteLine()的区别分析、css inline-block后有换行符变成空白间隔bug等相关知识的信息别忘了在本站进行查找喔。

本文标签: