首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell导出csv丢失断线

Powershell导出csv丢失断线
EN

Stack Overflow用户
提问于 2022-10-31 09:20:10
回答 1查看 67关注 0票数 0

我使用以下ps命令批量读取文件夹中每个txt文件的内容,然后将其导出到csv。它正常读取,但是文件内容变成一行,所有换行符都丢失了。

ls |Select-Object -Property {Get-Content $_.FullName -Encoding utf8} | Export-Csv "a.csv" -Encoding UTF8

我希望它在导出时保持格式(主要是空格和换行符)。

输入和输出:在这里输入图像描述

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-31 12:36:27

  • 或者:使用-RawGet-Content一起确保每个文件被读取为一个单一的、多行的string.Note。使用-Raw Get_ Select-Object -Property { Get-Content -Raw $_.FullName -Encoding }>导出-Csv "a.csv“-Encoding utf8。
  • 或者,如果您想控制换行符格式,以及每个文件的行是否应该有尾随换行符,请使用Get-Content不带-Raw,并使用-joinNote -join "n" (joining the lines with Unix-format LF-only newlines). # Each resulting string will have \*no\* trailing newline, but you # you can add it with ((Get-Content ...) -join "n") + "n" # Use "rn" for Windows-format CRLF newlines. Get-ChildItem | Select-Object -Property { (Get-Content $\_.FullName -Encoding utf8) -join "n“} Export-Csv "a.csv”-Encoding UTF8将生成的行数组与选择的换行符/序列连接起来。

注意,您的CSV文件:

  • 将有一个列,其字段为"..."-enclosed多行字符串。
代码语言:javascript
复制
- Use something like `Select-Object Name, { ... }` to also include each file's _name_.
代码语言:javascript
复制
- To give the column a better name, use a [hashtable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Hash_Tables) as the [calculated property](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Calculated_Properties), whose `Name` entry allows you to specify the name, and whose `Expression` entry must be your script block; e.g.:

.‘>选择-Object @{ Name=’FileContent‘;Expression={ Get-Content -Raw $_.FullName -Encoding utf8 }}.

至于,你尝试过

  • 当不使用-Raw时,Get-Content逐行地流文件的内容,当捕获该输出时,它将成为一个数组。
  • 计算出的属性的脚本块作为一个整体用于确定该属性的值,因此一个行数组确实是结果。
  • Export-Csv (或ConvertTo-Csv)遇到作为数组(或类似类型的集合)的属性值时,它会将其收缩,即必须将集合的表示形式创建为单个字符串。
代码语言:javascript
复制
- _Usually_, this results in _useless_ stringification simply by the collection's _type name_, e.g., in the case of an array, _verbatim_ `System.Object[]`
代码语言:javascript
复制
- However, if - as in your case - the collection happens to be wrapped in a usually _invisible_ `[psobject]` instance, a more helpful, albeit single-line stringification occurs: the (stringified) collection elements are joined _with spaces_ in effect, the newlines from your file are therefore replaced with spaces.
代码语言:javascript
复制
- The reason a `[psobject]` wrapper is present in your case is that PowerShell wraps output objects from a _pipeline_ that way - which includes output from a script block (`{ ... }`).

为说明上述情况:

代码语言:javascript
复制
# NO [psobject] wrapper -> CSV serialization by *type name*:
PS> [pscustomobject] @{ ArrayProp = 'one', 'two' } | ConvertTo-Csv

"ArrayProp"
"System.Object[]"  # !!
代码语言:javascript
复制
# WITH [psobject] wrapper -> CSV serialization by 
# *space-concatenated elements*:
PS> [pscustomobject] @{ ArrayProp = [psobject] ('one', 'two') } | ConvertTo-Csv

"ArrayProp"
"one two"  # !!
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74261118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档