csv文件导入问题 [英] csv file import problem

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

问题描述

你好


如果在读取csv文件时使用c文件io函数,如果

字段包含嵌入式回车符,则会出现问题。所以我可以查看撇号

对,但处理似乎有点困难。有没有人有一个强大的

算法或函数用于从csv

文件中可靠地获取整行文本,即使某些字段包含嵌入的回车符?


Angus

解决方案

如果读取csv文件,使用c文件io函数会出现问题,如果


字段包含嵌入的回车符。所以我可以查看撇号

对,但处理似乎有点困难。有没有人有一个强大的

算法或函数用于从csv

文件中可靠地获取整行文本,即使某些字段包含嵌入的回车符?



我不确定你用撇号表示什么符号或它是什么

与回车或csv(无论那个)有什么关系is)文件。

我怀疑即使我确实知道所有我仍然无法提供建议因为你没有提供足够的细节

关于你想要实现的目标。


所以你有一些csv在您的平台上的文件,其中行是

没有(仅)回车。此文件包含

行,其中包含一个或多个CR。你想把

行放到某个数组中还是比这更精细的东西?


csv文件的规则是每个''字段''用逗号(,)和

分隔,每个''记录'的结尾由回车终止。


如果某个字段包含回车符,则将该字段括在和

" - 引号。 (对不起撇号是错误的使用条款)。


如果我有这个记录:


1,1纽约大道, 345666,你好,在这个领域我们有一个马车

返回


更多文字在这里,0233-444-7777,蓝色


如果我使用


char * fgets(char * s,size_t n,FILE * stream);


然后fgets在两个之间进行回车。 我和我只是记录的一半。


我显然可以检查字符串中是否有奇数个'和'如果是这样的话,请到下一个

回车 - 但看起来确实很难得到它。我想知道

如果有人之前遇到过这种无关紧要的东西以及它们是如何绕过的那样

呢?


" Spiros Bousbouras" < sp **** @ gmail.comwrote in message

news:11 ********************* @ h48g2000cwc.googlegro ups .com ...


如果读取csv文件,则使用c文件io函数有问题



如果


字段包含嵌入式回车。所以我可以检查



撇号


对,但处理似乎有点挣扎。有没有人得到



健全


算法或函数用于从
可靠地获取整行文本



csv


文件,即使某些字段包含嵌入的回车符?



我不知道你用撇号表示什么符号或它是什么

与回车或csv(无论那个)有什么关系is)文件。

我怀疑即使我确实知道所有我仍然无法提供建议因为你没有提供足够的细节

关于你想要实现的目标。


所以你有一些csv在您的平台上的文件,其中行是

没有(仅)回车。此文件包含

行,其中包含一个或多个CR。你想把

行放到某个数组或更精细的东西吗?



有人写道:


csv文件的规则是每个''字段''用逗号分隔(,)和

结束每个''记录''由回车终止。


如果一个字段包含一个回车符,那么你将该字段括在和

" - 引号。 (对不起撇号是错误的使用条款)。


如果我有这个记录:


1,1纽约大道, 345666,你好,在这个领域我们有一个马车

返回


更多文字在这里,0233-444-7777,蓝色


如果我使用


char * fgets(char * s,size_t n,FILE * stream);


然后fgets在两个之间进行回车。 我和我只是记录的一半。


我显然可以检查字符串中是否有奇数个'和'如果是这样的话,请到下一个

回车 - 但看起来确实很难得到它。我想知道

如果有人之前遇到过这种无关紧要的事情以及它们是如何绕过的那样

呢?



这可能对您有用:

< ftp://ftp.iiug.org/pub/informix/pub/unl_utils -1.0.tgz>


具有以下功能...


char_esc * fgets_csv(char_esc * s,size_t n,FILE * stream) ;

int fputs_csv(const char_esc * s,FILE * stream);


int count_cols(const char_esc * s);

size_t get_col(const char_esc * s,int colno,char * col,size_t n);


-

rh


Hello

Using the c file io functions if reading a csv file there is a problem if a
field contains an embedded carriage return. So I can check for apostrophe
pairs but processing does seem a bit of a struggle. Has anyone got a robust
algorithm or functions to use to reliably get a whole row of text from a csv
file even if some fields contain embedded carriage returns?

Angus

解决方案

Using the c file io functions if reading a csv file there is a problem if a

field contains an embedded carriage return. So I can check for apostrophe
pairs but processing does seem a bit of a struggle. Has anyone got a robust
algorithm or functions to use to reliably get a whole row of text from a csv
file even if some fields contain embedded carriage returns?

I''m not sure what symbol you mean by apostrophe or what it
has to do with carriage returns or csv (whatever that is) files.
I suspect that even if I did know all that I still wouldn''t be able
to offer suggestions because you''re not giving enough details
about what you''re trying to achieve.

So you have some "csv" file on your platform where lines are
not terminated by (only) a carriage return. This file contains
lines which contain one or more CRs. Do you want to put the
line into some array or something more elaborate than that ?


Rules for a csv file are that each ''field'' is separated by a comma (,) and
the end of each ''record'' is terminated by a carriage return.

If a field contains a carriage return then you enclose the field in " and
" - quote marks. (Sorry apostrophe was wrong term to use).

So if I have this record:

1,1 The Avenue, New York, 345666,"hello in this field we have a carriage
return

more text here", 0233-444-7777,blue

If I use

char *fgets(char *s, size_t n, FILE *stream);

Then fgets gets to carriage return between two " "''s and I only half part of
the record.

I can obviously check if odd number of "''s in string and if so get to next
carriage return - but it does seem tricky getting it just right. I wondered
if anyone had come across this sort nof thing before and how they got round
it?

"Spiros Bousbouras" <sp****@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...

Using the c file io functions if reading a csv file there is a problem

if a

field contains an embedded carriage return. So I can check for

apostrophe

pairs but processing does seem a bit of a struggle. Has anyone got a

robust

algorithm or functions to use to reliably get a whole row of text from a

csv

file even if some fields contain embedded carriage returns?


I''m not sure what symbol you mean by apostrophe or what it
has to do with carriage returns or csv (whatever that is) files.
I suspect that even if I did know all that I still wouldn''t be able
to offer suggestions because you''re not giving enough details
about what you''re trying to achieve.

So you have some "csv" file on your platform where lines are
not terminated by (only) a carriage return. This file contains
lines which contain one or more CRs. Do you want to put the
line into some array or something more elaborate than that ?



Someone wrote:

Rules for a csv file are that each ''field'' is separated by a comma (,) and
the end of each ''record'' is terminated by a carriage return.

If a field contains a carriage return then you enclose the field in " and
" - quote marks. (Sorry apostrophe was wrong term to use).

So if I have this record:

1,1 The Avenue, New York, 345666,"hello in this field we have a carriage
return

more text here", 0233-444-7777,blue

If I use

char *fgets(char *s, size_t n, FILE *stream);

Then fgets gets to carriage return between two " "''s and I only half part of
the record.

I can obviously check if odd number of "''s in string and if so get to next
carriage return - but it does seem tricky getting it just right. I wondered
if anyone had come across this sort nof thing before and how they got round
it?

This might be useful to you:
<ftp://ftp.iiug.org/pub/informix/pub/unl_utils-1.0.tgz>

Has these functions ...

char_esc *fgets_csv(char_esc *s, size_t n, FILE *stream);
int fputs_csv(const char_esc *s, FILE *stream);

int count_cols(const char_esc *s);
size_t get_col(const char_esc *s, int colno, char *col, size_t n);

--
rh


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

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