解析CSV文件的问题 [英] Problem Parsing CSV File

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

问题描述




m使用拆分功能来解析csv文件.....我在一行文件中出现问题,如下所示:

BLUEUSBXM,X-MICRO(XBT-DG5R(C)R1)蓝牙USB DONGLE PLUS,CLASS 1,360,0.00,, ;",0,0.00,""," T1","",5.58," 4000",8.50,0.00,< 02/10/2006",0.00," 16/04 / 2007"


现在的问题是它解析了第一个字段正确。

但是

不解析第二个字段.....

avars = split(line,",")

应该给:

avars(1)=" X-MICRO(XBT-DG5R(C)R1)BLUETOOTH USB DONGLE PLUS,CLASS 1"


而不是它给我:

avars(1)=" X-MICRO(XBT-DG5R(C)R1)BLUETOOTH USB DONGLE PLUS"


我该如何解决这个问题....


谢谢

Pindoriya1

解决方案

Split函数工作正常 - 每次找到逗号时都会拆分文件,并且在PLUS之后有一个逗号。


为了解决这个问题你可以确保输入CSV文件的所有字段都删除了任何逗号,或者你可以用不同的方式解析文件。


B博士


Split函数工作正常 - 每次找到逗号时都会拆分文件,并且在PLUS之后有一个逗号。


要解决此问题,您可以确保输入CSV文件的所有字段都删除了任何逗号,或者您可以用不同的方式解析文件。


博士B




谢谢我知道这就是为什么我想要解决方案......

实际上该文件来自帐户软件因此我们在软件中进行了更改或者我们一次解决了这个问题,并且我们在任何地方都可以获得这样的供稿.....


i更喜欢第二种方式,而不是告诉accountign人们更改名称....我想要解决方案,我可以在任何地方使用我得到这种情况................


你的建议我可以用不同的方式分享文件....

所以你能建议我这样的一两个s ....


谢谢

pindoriya1


我唯一可以想到你可能是能够在这里做的是循环检查每个字符的字符串,看看它是否是逗号。如果它是一个逗号然后检查下一个字符是否是一个空格 - 如果是这样那么它将在你的一个字段中而不是你的一个分隔符中的逗号,所以你可以忽略它。


这不是万无一失的,因为有些人可能会输入一些逗号后面没有空格的文本,但它会产生比现有方法更好的结果。


B博士


Hi,

m using split function to parse the csv file ..... i m getting problem in one line of file which looks like this :

"BLUEUSBXM","X-MICRO (XBT-DG5R (C) R1) BLUETOOTH USB DONGLE PLUS, CLASS 1",360,0.00,"","",0.00,"",0,0.00,"","T1","",5.58," 4000",8.50,0.00,"02/10/2006",0.00,"16/04/2007"


now the problem is it parses first field properly.
BUT
Not parsing second field.....
avars = split(line,",")
should give:
avars(1) = "X-MICRO (XBT-DG5R (C) R1) BLUETOOTH USB DONGLE PLUS, CLASS 1"

instead of that it gives me:
avars(1) = "X-MICRO (XBT-DG5R (C) R1) BLUETOOTH USB DONGLE PLUS"

how can i solve this problem....

Thanks
Pindoriya1

解决方案

The Split function is working correctly - it is splitting the file every time it finds a comma and there is a comma after the word PLUS.

To solve this you could make sure that all the fields that are entered into the CSV file have any comma''s removed or you could parse the file in a different way.

Dr B


The Split function is working correctly - it is splitting the file every time it finds a comma and there is a comma after the word PLUS.

To solve this you could make sure that all the fields that are entered into the CSV file have any comma''s removed or you could parse the file in a different way.

Dr B



Thanks I know that that is why i want solution for that.....
actually the file comes from account software SO EITHER we change in the software OR we solve this problem once and for alll the time we get such feeds form anywhere.....

i prefer second way, as rather than telling accountign people to change name.... i want solution which i can use everywhere i get this situation................

YOU SUGGEST I COULD PARSE FILE IN DIFFERENT WAY....
so can you suggest me one or two of such ways....

thank you
pindoriya1


The only thing I can think of that you might be able to do here is to loop through the string checking each character to see whether it''s a comma. If it is a comma then check the next character to see whether it is a space - if so then it''s going to be a comma inside one of your fields rather than one of your delimiters so you can ignore it.

It''s not foolproof as somebody might enter some text which had a comma without a space after it but it will yield better results than the existing method.

Dr B


这篇关于解析CSV文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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