Visios

Hello,

I get CSV file like this from a C# application.

WebClient wc = new WebClient();
string uri = rpturl.appURL;
string fname = "c:/data_file.csv";
wc.DownloadFile(uri,fname);

The CSV file i get from application got 6 fields


ID FNAME MNAME LNAME MARK SUBJECT

I want only 5 field output from CSV in this order.

ID LNAME FNAME SUBJECT MARK


I have another application looking for fields in the following order.How can i make CSV like this using C# code .


Please help me with code. I never done anything with CSV before.

Thanks



Re: Visual C# General Format CSV using C#

H. Tony

csv is just coma delimited plain text file. if the field contains coma, you cna use double quote around it.

such as:

1024, "test, another test", nothing

so basically you need to parse/write such format. if there's no ',' coma in your fields, it's just trivial.

you can read each line, use string.split(',') to get all fields into string[]

then you can use StreamWriter.Writeline(string.format("{0},{1},{2},{3},{4}",fields[0],fields[3],fields[5],fields[4]) to convert the 6 fields to 5 fields as you presented above.





Re: Visual C# General Format CSV using C#

Visios

Thanks Tony. Got some idea.

Please post real sample.

never did so much c# coding.





Re: Visual C# General Format CSV using C#

timvw

Tony did post some sample code, now it's up to you to put some effort in it... (And if that doesn't work, post the code you've tried, and tell us what is going wrong... But don't expect us to do your homework.)





Re: Visual C# General Format CSV using C#

Yogesh Pawar

Hi

I will suggest you to try out logparser. You can do almost everything with CSVs using that.

I dont know if latest still doesn't allow for joing two files with different formats but I have a work around on that so let me know.

Also http://www.codeproject.com/cs/database/CsvReader.asp might help.





Re: Visual C# General Format CSV using C#

Visios

Thanks yogesh.

What is logparser . How can i do .

Do u have a code sample.

I got CSV file getting from an application...i need to remove some fields and reorder the CSV file.

Add a new row on the top of CSV file.

This changed CSV file will be cosumed by anther application. So i need to format CSV for the new application.

How can i open a CSV file, parse through CSV file.

Write only required columns back to CSV file.

Add new row to new CSV file on the top with some string name .

Experts, Please post a code sample. I am C# begineer.





Re: Visual C# General Format CSV using C#

H. Tony

for the simple case without double quote:

Code Snippet

StreamWriter writer = new StreamWriter("output.csv");
string[] lines = File.ReadAllLines("input.csv");
foreach(string line in lines){

string[] fields = line.Split(',');
writer.Writeline(string.format("{0},{1},{2},{3},{4}",fields[0],fields[3],fields[5],fields[4])
}
writer.close();

for large file, you probably want to read each line using streamreader, for complicated csv file handling, please try 3rd party code

Visios wrote:

Thanks yogesh.

What is logparser . How can i do .

Do u have a code sample.

I got CSV file getting from an application...i need to remove some fields and reorder the CSV file.

Add a new row on the top of CSV file.

This changed CSV file will be cosumed by anther application. So i need to format CSV for the new application.

How can i open a CSV file, parse through CSV file.

Write only required columns back to CSV file.

Add new row to new CSV file on the top with some string name .

Experts, Please post a code sample. I am C# begineer.







Re: Visual C# General Format CSV using C#

Yogesh Pawar

Hi

Logparser provides SQL like access to log files usually CSV etc. Please google 'logparser'. It was Microsoft internal tool but can downloaded now. From your problem I think you can just create a new file with whatever column you want by querying through logparser.

(http://www.microsoft.com/downloads/details.aspx FamilyID=890CD06B-ABF8-4C25-91B2-F8D975CF8C07&displaylang=en)

Regards

Yogesh





Re: Visual C# General Format CSV using C#

Visios

thanks tony.

i modified the code according to my requirement....this code is not working...

How can i make it work ... Please let me know.

StreamWriter writer = new StreamWriter("//server/app/output.csv");

string[] lines = File.ReadAllLines("//server/app/input.csv");

foreach(string line in lines){

string[] fields = line.Split(',');

writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26},{27},{28},{29},{30},{31},{32},{33},{34},{35},{36}", fields[0], fields[1], fields[2], fields[22], fields[21], fields[31]));

}

writer.Close();





Re: Visual C# General Format CSV using C#

H. Tony

Code Block

StreamWriter.WriteLine(...)
string.Format(...)
StreamWriter.Close(...)


The code is not verified but I think it should be correct, despite case errors like above.

Visios wrote:

thanks tony.

i am getting this errors....why i get this error .

Error 2 'System.IO.StreamWriter' does not contain a definition for 'Writeline'
Error 3 'string' does not contain a definition for 'format'
Error 4 'System.IO.StreamWriter' does not contain a definition for 'close'






Re: Visual C# General Format CSV using C#

Visios

thanks tony.

i fixed the case problem.

But code is not working...

i am getting blank csv file as output.

how can i read mutiple line of csv file data and re-order columns and write back to output csv file .





Re: Visual C# General Format CSV using C#

H. Tony

so it compiles now does it throw exception

the idea is there, but you need to make it work. I see the {0} input argument list count doesn't match that in the format string in your code above.

if you manage to get it to run, try put a break point at WriteLine so you can see what the data is per line and what is about to be written.

Visios wrote:

thanks tony.

i fixed the case problem.

But code is not working...

i am getting blank csv file as output.

how can i read mutiple line of csv file data and re-order columns and write back to output csv file .






Re: Visual C# General Format CSV using C#

Visios

When i try this code.....i am getting all first field in the output file....all other fields are empty...why so .

it is reading full time...but it is outputting only first field.....all other fields are empty in output file.

StreamWriter writer = new StreamWriter("output.csv");

string[] lines = File.ReadAllLines("input.csv");

foreach(string line in lines)

{

string[] fields = line.Split(',');

writer.WriteLine(string.Format(fields[0],fields[1],fields[2],fields[22],fields[21],fields[31]));

}

writer.Close();





Re: Visual C# General Format CSV using C#

H. Tony

this line looks wrong without the red part.

writer.WriteLine(
string.Format("{0},{1},{2},{3},{4},{5}",fields[0],fields[1],fields[2],fields[22],fields[21],fields[31]));


Visios wrote:

When i try this code.....i am getting all first field in the output file....all other fields are empty...why so .

it is reading full time...but it is outputting only first field.....all other fields are empty in output file.

StreamWriter writer = new StreamWriter("output.csv");

string[] lines = File.ReadAllLines("input.csv");

foreach(string line in lines)

{

string[] fields = line.Split(',');

writer.WriteLine(string.Format(fields[0],fields[1],fields[2],fields[22],fields[21],fields[31]));

}

writer.Close();






Re: Visual C# General Format CSV using C#

Visios

If i add red part...i am getting wrong field values.......

why so .

How u control field order .