如何读取.csv文件分隔除逗号以外的列 [英] How to read .csv file separating the column other than a comma

查看:96
本文介绍了如何读取.csv文件分隔除逗号以外的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

< span style ="font-size:medium">我用以下语句打开一个电子表格(.csv)文件:

打开folderNameInput& fileName用于输入为FileNbr

 

然后我使用以下语句读入VBA:

行输入#FileNbr,textRow

 

我收到的输入(在textRow中)是由多列组成的一行,每行用逗号(,)分隔。

 

但是,我需要将列中的一个数据(比如第5列输入)拆分,然后写入另一个输出".csv"文件中的多个列。不幸的是,在
5列中也存在一个逗号,我不应该拆分,所以我不能根据逗号的存在进行拆分,否则第5列输入中的逗号之间没有区别,或者这是逗号分隔我读过的专栏。

 

我在想是否可以单独阅读每一列,所以我可以用第5列输入替换逗号在使用分隔列的
a逗号之外的第5列输入分割之前,未使用的字符(可能是〜)。这样,当我基于逗号分割时,输入列5将不会被拆分,然后我可以在拆分后将~shown替换为输出中的逗号。

 

To get my point across, here is an example after reading what my data looks like:

Column1,column2,column3,column4, colu,mn5 ,column6
- >因为column5包含一个逗号。

 

如果我根据逗号分割,那么结果将是:

Column1

column2

column3

column4

colu

mn5

column6

 

这不是我想要的。

oemar01

推荐答案

希望我能解释得令人满意。

Hope I can explain the satisfactorily.

如果csv文件中的任何字段包含逗号作为字段的一部分,则该字段用双引号括起来。

If any field in a csv file contains a comma as part of the field then the field is enclosed in double quotes.

如果用记事本打开文件,你会看到双引号。(首先打开记事本,然后将过滤器设置为所有文件,这样你就可以看到csv文件,然后打开它)

If you open the file with Notepad then you will see the double quotes. (Open notepad first and then set the filter to all files so you can see the csv file and then open it)

以下代码的说明:

从csv文件中读取一行。

Reads one line from the csv file.

查找该行中的第一个双引号。

Finds the first double quote in the line.

用双垂直线替换双引号后面的第一个逗号。请注意,执行此操作的代码行必须将字符串的左侧连接到带有由"替换"命令创建的字符串的双引号,因为从
第一个双引号开始替换。

Replaces the first comma after the double quote with a vertical line. Note that the line of code to do this must concatenate the left of the string up to the double quote with the string created by the Replace command because replace starts from the first double quote.

我已将可选行添加到 删除通过用空字符串替换双引号。必须在找到双引号然后用垂直线替换下面的逗号后执行此操作。我建议你留下这一行注释
out,这样你就可以看到输出到立即窗口的双引号然后取消注释该行并重新运行并删除双引号。

I have included an optional line to remove the double quotes by replacing with null strings. This must be done after finding the double quotes and then replacing the following comma with a vertical line. I am suggesting that you leave this line commented out so you can see the double quotes in the output to the Immediate window and then uncomment the line and re-run with the double quotes removed.

结果字符串然后被剩余的逗号分割并分配给数组。

The resultant string is then split by the remaining commas and assigned to an array.

代码将数组内容输出到立即窗口。

The code will output the array contents to the Immediate window.

子测试()

    Dim FileNbr As Long

    Dim strPath As String

    Dim strFile As String

    Dim strTextRow As String

    Dim arrFields As Variant

    Dim i As Long

   

    FileNbr = 1     '设置为1用于测试目的

    strPath = ThisWorkbook.Path& " \"&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; '编辑到您的路径

    strFile ="使用数据commas.csv测试csv"    '编辑到您的文件名

   

   打开strPath& strFile For Input As #FileNbr

   

   行输入#FileNbr,strTextRow

   

    i = InStr(1,strTextRow,"""")'查找第一个双重报价

   

    '下一行将在双引号后替换第一个逗号

    '注意只替换返回双引号中的文字结束

    '需要在双引号中连接双引号的左边

    '在第一次双引号后用垂直线替换逗号

    strTextRow = Left(strTextRow,i - 1)&替换(strTextRow,","," |",i,1)

   

    '下一个命令行将删除双引号

    '首先测试没有这一行,这样你就可以看到输出中的双引号

    'strTextRow =替换(strTextRow,"""","")

   

    arrFields = Split(strTextRow,",")

   

   对于i = LBound(arrFields)到UBound(arrFields)

        Debug.Print arrFields(i)

   接下来我是
   

   关闭#FileNbr

   

End Sub

Sub test()
    Dim FileNbr As Long
    Dim strPath As String
    Dim strFile As String
    Dim strTextRow As String
    Dim arrFields As Variant
    Dim i As Long
   
    FileNbr = 1     'Set to 1 for testing purposes
    strPath = ThisWorkbook.Path & "\"       'Edit to your path
    strFile = "Testing csv with data commas.csv"    'Edit to your file name
   
    Open strPath & strFile For Input As #FileNbr
   
    Line Input #FileNbr, strTextRow
   
    i = InStr(1, strTextRow, """") 'Find the first double quote
   
    'Next line will Replace first comma after double quote
    'Note Replace only returns text from the double quote to end
    'Need to concatenate left of the double quote with right from the double quote
    'Replaces the comma after first double quote with a vertical line
    strTextRow = Left(strTextRow, i - 1) & Replace(strTextRow, ",", "|", i, 1)
   
    'Next command line will remove the double quotes
    'Test without this row first so you can see the double quotes in the output
    'strTextRow = Replace(strTextRow, """", "")
   
    arrFields = Split(strTextRow, ",")
   
    For i = LBound(arrFields) To UBound(arrFields)
        Debug.Print arrFields(i)
    Next i
   
    Close #FileNbr
   
End Sub


这篇关于如何读取.csv文件分隔除逗号以外的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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