473,325 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

csv read write

Hi All,

I searched the web to see how i can read csv file (specific cells) but
didn't find much.
Can anyone give me a link or help how to read or write to a specific
cell in csv file?
Using SQL can be great.

TIA.

Jan 3 '07 #1
11 3747
I generally dont search my csv files - I generally parse the entire file for
an import or something.

But basically what I do is load the file into a StringReader and then split
each line by the delimited to get each individual cell.

eg:
reader = new StreamReader( FileUploadImport.PostedFile.InputStream);
while (reader.Peek() >= 0) //not eof
{
String line = reader.ReadLine();
String[] items = line.Split(',');
//now you can get the cells
String name = items[0];
}

Jan 3 '07 #2
you can reference Interop.Excel Com object and use
oXL.Workbooks.OpenText
where oXL is the Excel.Application.

you can loop through the Usedrange to get the no. of rows and columns used.

You have Cells.Offset or Cell.Select to get the individual cell.

Regards,
Vincent

"XOR" wrote:
I generally dont search my csv files - I generally parse the entire file for
an import or something.

But basically what I do is load the file into a StringReader and then split
each line by the delimited to get each individual cell.

eg:
reader = new StreamReader( FileUploadImport.PostedFile.InputStream);
while (reader.Peek() >= 0) //not eof
{
String line = reader.ReadLine();
String[] items = line.Split(',');
//now you can get the cells
String name = items[0];
}
Jan 3 '07 #3

XOR wrote:
I generally dont search my csv files - I generally parse the entire file for
an import or something.

But basically what I do is load the file into a StringReader and then split
each line by the delimited to get each individual cell.

eg:
reader = new StreamReader( FileUploadImport.PostedFile.InputStream);
while (reader.Peek() >= 0) //not eof
{
String line = reader.ReadLine();
String[] items = line.Split(',');
//now you can get the cells
String name = items[0];
}
Hello XOR and thanks for the reply,

Your solution won't work if user insert ',' into cell as text.
What can i do about it?

Jan 3 '07 #4

Vincy wrote:
you can reference Interop.Excel Com object and use
oXL.Workbooks.OpenText
where oXL is the Excel.Application.

you can loop through the Usedrange to get the no. of rows and columns used.

You have Cells.Offset or Cell.Select to get the individual cell.

Regards,
Vincent

"XOR" wrote:
I generally dont search my csv files - I generally parse the entire file for
an import or something.

But basically what I do is load the file into a StringReader and then split
each line by the delimited to get each individual cell.

eg:
reader = new StreamReader( FileUploadImport.PostedFile.InputStream);
while (reader.Peek() >= 0) //not eof
{
String line = reader.ReadLine();
String[] items = line.Split(',');
//now you can get the cells
String name = items[0];
}

Hi Vincy,

thanks for your reply also.
Is these com object called oXL.Workbooks.OpenText that i can reference
to? Like
Microsoft Office Excel 11.0?

Jan 3 '07 #5
On Tue, 2 Jan 2007 23:37:00 -0800, Vincy <Vi***@discussions.microsoft.com>
wrote:
>you can reference Interop.Excel Com object and use
oXL.Workbooks.OpenText
where oXL is the Excel.Application.

you can loop through the Usedrange to get the no. of rows and columns used.

You have Cells.Offset or Cell.Select to get the individual cell.

Regards,
Vincent

"XOR" wrote:
>I generally dont search my csv files - I generally parse the entire file for
an import or something.

But basically what I do is load the file into a StringReader and then split
each line by the delimited to get each individual cell.

eg:
reader = new StreamReader( FileUploadImport.PostedFile.InputStream);
while (reader.Peek() >= 0) //not eof
{
String line = reader.ReadLine();
String[] items = line.Split(',');
//now you can get the cells
String name = items[0];
}
However, as revealed in your previous post where you ask how to use a semicolon
for a delimiter, you lose the ability to tailor the reading of the file(s) to
your specifications.

It is better to write the parsing code yourself unless you are OK with the
defaults of some ready made solution.

Parsing delimited files as XOR has shown you, is not at all difficult at all,
and is something every developer should learn to do proficiently .

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Jan 3 '07 #7
I agree with the general statement, but anybody who thinks that
parsing CSV (specifically) is trivial hasn't read the full CSV spec...
in the *full* case, since it has disparate escape sequences for
different scenarios (such as quoted and unquoted, multiline, etc) it
can be just painful. And don't forget that some internationalisations
of CSV apps use semicolon instead of comma by default (Excel in French
IIRC). If you know that you only use a subset of the spec then you can
get away with simpler options like Split- and Regex-based solutions.
The codeproject link I posted claims to deal with most scenarios
including choice of delimiter (I'm not vouching for the code, but
looks useful).

Similar to parsing an e-mail address; sounds easy in the
jo********@somewhere.com sense, but soon gets scary if you really read
the spec... comments... folding whitespace... uri endpoints (e-mail
addresses without an @), etc...

Marc
Jan 3 '07 #8

Otis Mukinfus wrote:
On Tue, 2 Jan 2007 23:37:00 -0800, Vincy <Vi***@discussions.microsoft.com>
wrote:
you can reference Interop.Excel Com object and use
oXL.Workbooks.OpenText
where oXL is the Excel.Application.

you can loop through the Usedrange to get the no. of rows and columns used.

You have Cells.Offset or Cell.Select to get the individual cell.

Regards,
Vincent

"XOR" wrote:
I generally dont search my csv files - I generally parse the entire file for
an import or something.

But basically what I do is load the file into a StringReader and then split
each line by the delimited to get each individual cell.

eg:
reader = new StreamReader( FileUploadImport.PostedFile.InputStream);
while (reader.Peek() >= 0) //not eof
{
String line = reader.ReadLine();
String[] items = line.Split(',');
//now you can get the cells
String name = items[0];
}

However, as revealed in your previous post where you ask how to use a semicolon
for a delimiter, you lose the ability to tailor the reading of the file(s) to
your specifications.

It is better to write the parsing code yourself unless you are OK with the
defaults of some ready made solution.

Parsing delimited files as XOR has shown you, is not at all difficult at all,
and is something every developer should learn to do proficiently .

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Can't I do it with SQL? SQL doesn't know how to get specific cell(by
cell/row number)?

Jan 4 '07 #9
On Wed, 3 Jan 2007 12:09:12 -0000, "Marc Gravell" <ma**********@gmail.com>
wrote:
>I agree with the general statement, but anybody who thinks that
parsing CSV (specifically) is trivial hasn't read the full CSV spec...
in the *full* case, since it has disparate escape sequences for
different scenarios (such as quoted and unquoted, multiline, etc) it
can be just painful. And don't forget that some internationalisations
of CSV apps use semicolon instead of comma by default (Excel in French
IIRC). If you know that you only use a subset of the spec then you can
get away with simpler options like Split- and Regex-based solutions.
The codeproject link I posted claims to deal with most scenarios
including choice of delimiter (I'm not vouching for the code, but
looks useful).

Similar to parsing an e-mail address; sounds easy in the
jo********@somewhere.com sense, but soon gets scary if you really read
the spec... comments... folding whitespace... uri endpoints (e-mail
addresses without an @), etc...

Marc
Marc,

Now you've found me out ;o)

I didn't know there was a spec for CSV files.

I do a lot of backend file parsing and have written several routines to handle
all the anomalies (known to me) of parsing delimited files (I wish we would stop
saying "CSV files", because as most of you know, there are many many more
delimiters used in delimited files).

Can you give me a link to the spec you mentioned?

Thanks,

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Jan 4 '07 #10
It evolved one over time, like fungus. RFC4180 would be a good start:
http://tools.ietf.org/html/rfc4180. But as we are aware, various
implementations over the years lead to all sorts of nuances on this,
including (but not limited to) the semi-colon debacle, line-ending
disparity (note RFC2046 specifies CRLF for all MIME "text" subtypes,
and RFC4180 declares CSV as text/csv, hence CRLF, but LF, CR, and LFCR
should all really be handled for compatibility), and various twists on
quoting.

Marc
Jan 4 '07 #11
On Thu, 4 Jan 2007 12:57:16 -0000, "Marc Gravell" <ma**********@gmail.com>
wrote:
>It evolved one over time, like fungus. RFC4180 would be a good start:
http://tools.ietf.org/html/rfc4180. But as we are aware, various
implementations over the years lead to all sorts of nuances on this,
including (but not limited to) the semi-colon debacle, line-ending
disparity (note RFC2046 specifies CRLF for all MIME "text" subtypes,
and RFC4180 declares CSV as text/csv, hence CRLF, but LF, CR, and LFCR
should all really be handled for compatibility), and various twists on
quoting.

Marc
Thanks, Mark!

I once did a little research on the data residing in the AS400 at our shop and
found that in all fields entered by users there was at least one usage of each
character one can type from a keyboard. That makes parsing a challenge indeed!

The data validation rule for given our AS400 developers is thus, "Don't do any
validation that slows the user down, even if it is obviously bad, because it
costs us money.". Of course that edict was given by someone who believes
supporting such systems has no cost because they have no charge-back system in
place.

Most of my work and the work of my fellow developers is trying to track down
system problems caused by invalid data that comes to our backends from the
AS400. I'm just finishing a 3 month project that is designed to find and
correct those invalid data entries in between the AS400 and the systems that are
fed by the AS400 data. And you know that didn't cost anything at all. ;o)

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Jan 4 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
6
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
6
by: Juan Manuel Ramollino | last post by:
Greetings everyone! I am creating a webcontrol that reads one or more directorie's content and displays all the filenames (tipically PDFs or PPTs) so that the user can select the desired one and...
5
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
1
by: Arpan | last post by:
The contents of a text file are as follows: The Quick Brown Fox Jumped Over The Lazy Dog. Note that there isn't any space at the end of each of the 3 lines. Now when I do this:
8
by: dosworldguy | last post by:
I have been having a very peculiar issue from a long time. I have an application where multiple clients read from a shared set of files. When a record is changed, sometimes the win9x clients...
1
by: vinothg | last post by:
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and...
23
by: asit dhal | last post by:
hello friends, can anyone explain me how to use read() write() function in C. and also how to read a file from disk and show it on the monitor using onlu read(), write() function ??????
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.