473,396 Members | 2,030 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,396 software developers and data experts.

Reading a CSV where comma's are interpreted as newlines.

110 100+
I'm writing a program that reads in a text file or a csv and for each line of text in the file it creates a new object of an aray:

Expand|Select|Wrap|Line Numbers
  1.                 string[] lines = File.ReadAllLines(@newobject.csvDirectory);
  2.  
What I need to do is tell my code that if it encounters a comma that it needs to treat it as a new line marker. "\n"

At first I thought I would run a replace statement saying

Expand|Select|Wrap|Line Numbers
  1. line[i].replace(",","\n");
  2.  
but it is putting in a literal "\n" which is kicking out illegal char errors. How do I tell my test that if there is a comma consider it a new line?

That way a line like this:

Expand|Select|Wrap|Line Numbers
  1. This is line 1,This is line 2, This is line3. And more 3.
  2.  
will create 3 objects in my array.

Thanks in advance.
Jul 12 '10 #1

✓ answered by Christopher Nigro

Something like this:

Expand|Select|Wrap|Line Numbers
  1. string[] lines = null;
  2.  
  3.  using (StreamReader sr = new StreamReader(@"..\..\Sample.txt"))
  4. {
  5.     string fileContents = sr.ReadToEnd();
  6.     lines = fileContents.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
  7.  }
  8.  

3 1604
Use a StreamReader and call its ReadToEnd method to get a string. Then split that string on a comma to get your string array.
Jul 12 '10 #2
Fuzz13
110 100+
I'm confused as to how I read the data, split the data at the comma's, then have it reread everything with the newlines at commas made so it creates the appropriate number of objects in the array.
Jul 12 '10 #3
Something like this:

Expand|Select|Wrap|Line Numbers
  1. string[] lines = null;
  2.  
  3.  using (StreamReader sr = new StreamReader(@"..\..\Sample.txt"))
  4. {
  5.     string fileContents = sr.ReadToEnd();
  6.     lines = fileContents.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
  7.  }
  8.  
Jul 12 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Thomas Philips | last post by:
I have a data file that I read with readline(), and would like to control the formats of the lines when they are printed. I have tried inserting escape sequences into the data file, but am having...
5
by: JKop | last post by:
void Blah(int k) { k; } int main() { int a; int b; Blah( ++a, ++b ); }
3
by: Jeffrey E. Forcier | last post by:
This is a difficult issue to search for, and Googling (and reviewing the pertinent Python docs) has not found me anything useful. It's also not a super important issue, but regardless of whether...
3
by: SteelDetailer | last post by:
Thnaks in advance for considering this post. It's probably very simple, but..... I have an old VB6 application that allows me to create, save and edit a "project information file" that is a...
15
by: djj858 | last post by:
Another newbie question: How do I begin reading data, but starting from the xth line down a list? In other words, how do I skip the first lines and not read in those values?
8
by: Dalibor | last post by:
I have following code for putting contenst of a file into a web page: <?php $filename = "../msd/news.txt"; $handle = fopen ($filename, "r"); $content = fread ($handle, filesize($filename));...
2
by: JoeW | last post by:
Just wondering as I'm sure someone's done something similar to this before, I need to break up a string, broken up everytime a comma is come across. In example: A, B, C, D, E This can...
2
by: Edward K. Ream | last post by:
Hello all, I recently ran across a situation in which sax.saxutils.quoteattr did not work as I expected. I am writing Leo outlines as opml files http://en.wikipedia.org/wiki/OPML which forces...
3
by: Fraser Ross | last post by:
int a= 4, c= 0, x= 100, y= 200; c ? ++a, x : y ; // comma before ?: Should a be 5 at the end? If this is not an example of comma precedence over ?: can someone give one?
4
by: sherifffruitfly | last post by:
Hi all, I can't see what's wrong with this regex pattern: private int ParsePageViews(string str) { int ret = 0; string pattern = @"Visits.*\n\s*Total\s\.*\s(? <visits>(\d{3})|(\d,\d{3}))";
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.