【问题标题】:Export text content to text file without \n mark将文本内容导出到没有 \n 标记的文本文件
【发布时间】:2012-12-18 01:32:45
【问题描述】:

当我尝试导出字段的文本内容,并且该内容包含回车字符时,该字符会像 \N 字符串一样输出。

例如:

create table foo ( txt text );
insert into foo ( txt ) values ( 'first line
second line
...
and other lines');
copy foo ( txt ) to '/tmp/foo.txt';

我想返回以下(a):

first line
second line
...
and other lines

但是,输出是(b):

first line\Nsecond line\N...\Nand other lines

有人知道如何获得 (a) 输出吗?

【问题讨论】:

  • 你四处搜寻时发现了什么?

标签: sql postgresql copy text-files


【解决方案1】:

\N 来自于一行必须对应一个数据库行这一事实。

对于 CSV 格式,此规则放宽了,其中多行文本是可能的,但引号字符(默认情况下:")会将文本括起来。

如果您想要多行输出且周围没有封闭字符,则不应使用COPY,而应使用SELECT

假设调用者的执行环境是 unix shell,你可以这样做:

psql -A -t -d dbname -c 'select txt from foo' >/tmp/file.txt

【讨论】:

    【解决方案2】:

    你试过了吗:\r\n

    这是另一个可能有效的解决方案:

    E'This is the first part \\n And this is the second'
    

    通过https://stackoverflow.com/a/938/1085891

    另外,不要复制其他回复,请参阅此处:String literals and escape characters in postgresql

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多