CSV格式不正确? [英] Format of CSV not correct?

查看:297
本文介绍了CSV格式不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Powershell中使用EXPORT-CSV生成CSV,然后将其提供给Perl脚本。但是Perl无法导入该文件。



我已经验证了CSV文件对一个工作版本(已从同一个Perl脚本而不是powershell导出)和没有差别。这些列是完全相同的,它们都以分号作为分隔符。如果我在Excel中打开文件,但一切都在每行的第一个单元格(意思是我必须做一个文本到coloumns)。工作文件最后在不同的单元格从开始..



添加到混乱:当我在记事本中打开文件,并将内容复制/粘贴到新文件导入工作!



那么,我缺少什么?有没有隐藏属性,我不能发现与记事本?我必须更改编码类型吗?



请帮助:)

解决方案

要更好地查看CSV文件,请尝试使用记事本++ 。这将告诉你在状态栏中的文件编码。也打开隐藏的字符(视图>显示符号>显示所有字符)。这将显示是否只有换行符,或回车+换行符,制表符和空格等...您还可以从编码菜单更改文件编码。这可以帮助您识别差异。

更新 - 以下是如何将文本文件从Windows转换为Unix格式的代码:

/ p>

  $ allText = [IO.File] :: ReadAllText(C:\test.csv)-replace`r `n
$ encoding = New-Object System.Text.ASCIIEncoding
[IO.File] :: WriteAllText(C:\test2.csv,$ allText, $ encoding)

也可以使用Notepad ++(编辑> EOL转换> Unix格式) >

I am generating a CSV with EXPORT-CSV in Powershell and then feeding it to a Perl script. But Perl is unable to import the file.

I have verified the CSV-file against a working version (that has been exported from the same Perl-script and not powershell) and there are NO difference. The coloumns are excactly the same and they both have semicolon as delimiter. If I open the file in Excel however everything ends up in the first cell on each line (meaning I have to do a text-to-coloumns). The working file ends up in a different cells from the start..

To add to the confusion: when I open the file in notepad and copy/paste the contents to a new file the import works!

So, what am I missing? Are there "hidden" properties that I cannot spot with Notepad? Do I have to change the encoding-type?

Please help:)

解决方案

To get a better look at your CSV files try using Notepad++. This will tell you the file encoding in the status bar. Also turn on hidden characters (View > Show Symbol > Show All Characters). This will reveal if there are just line feeds, or carriage returns + line feeds, tabs vs spaces etc... You can also change the file encoding from the Encoding menu. This may help you identify the differences. Notepad doesn't display any of this information.

Update - Here's how to convert a text file from Windows to Unix format in code:

$allText = [IO.File]::ReadAllText("C:\test.csv") -replace "`r`n?", "`n" 
$encoding = New-Object System.Text.ASCIIEncoding    
[IO.File]::WriteAllText("C:\test2.csv", $allText, $encoding)

Or you can use Notepad++ (Edit > EOL Conversion > Unix Format).

这篇关于CSV格式不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆