Reading 'CSV' file - Cannot read csv file contains loong string - objective-c

I am using following method to read .csv file. It is working fine for csv files which contains normal strings, numbers ect.
But it fails to read csv files contains long strings (eg: description of 5,6 lines).
Is there is such technical difficulty? Your help is highly appreciated.
-(void)readTitleFromCSV:(NSString*)path AtColumn:(int)column
{
//arrBreeds = [[NSMutableArray alloc]init];
NSMutableArray *arrTemp = [[NSMutableArray alloc]init];
NSString *fileDataString=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *linesArray=[fileDataString componentsSeparatedByString:#"\n"];
// preparing array with questions and answers
for(int i=0; i<[linesArray count];i++){
[arrTemp addObject:[[linesArray objectAtIndex:i] componentsSeparatedByString:#","] ];
}
NSLog(#"ArrBreeds %#", arrTemp);
}

You are first separating the text from your CSV file based on line break and then separating the components. This happens something like this:
absd,sgsgs
dfdf,dfghj
Here you should get 3 objects after parsing , but with your code you will get 4.
You could just go on with this:
NSArray *linesArray=[fileDataString componentsSeparatedByString:#","];

Related

Replacing bad words in a string in Objective-C

I have a game with a public highscore list where I allow layers to enter their name (or anything unto 12 characters). I am trying to create a couple of functions to filter out bad words from a list of bad words
I have in a text file. I have two methods:
One to read in the text file:
-(void) getTheBadWordsAndSaveForLater {
badWordsFilePath = [[NSBundle mainBundle] pathForResource:#"badwords" ofType:#"txt"];
badWordFile = [[NSString alloc] initWithContentsOfFile:badWordsFilePath encoding:NSUTF8StringEncoding error:nil];
badwords =[[NSArray alloc] initWithContentsOfFile:badWordFile];
badwords = [badWordFile componentsSeparatedByString:#"\n"];
NSLog(#"Number Of Words Found in file: %i",[badwords count]);
for (NSString* words in badwords) {
NSLog(#"Word in Array----- %#",words);
}
}
And one to check a word (NSString*) agains the list that I read in:
-(NSString *) removeBadWords :(NSString *) string {
// If I hard code this line below, it works....
// *****************************************************************************
//badwords =[[NSMutableArray alloc] initWithObjects:#"shet",#"shat",#"shut",nil];
// *****************************************************************************
NSLog(#"checking: %#",string);
for (NSString* words in badwords) {
string = [string stringByReplacingOccurrencesOfString:words withString:#"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, string.length)];
NSLog(#"Word in Array: %#",words);
}
NSLog(#"Cleaned Word Returned: %#",string);
return string;
}
The issue I'm having is that when I hardcode the words into an array (see commented out above) then it works like a charm. But when I use the array I read in with the first method, it does't work - the stringByReplacingOccurrencesOfString:words does not seem to have an effect. I have traced out to the log so I can see if the words are coming thru and they are... That one line just doesn't seem to see the words unless I hardcore into the array.
Any suggestions?
A couple of thoughts:
You have two lines:
badwords =[[NSArray alloc] initWithContentsOfFile:badWordFile];
badwords = [badWordFile componentsSeparatedByString:#"\n"];
There's no point in doing that initWithContentsOfFile if you're just going to replace it with the componentsSeparatedByString on the next line. Plus, initWithContentsOfFile assumes the file is a property list (plist), but the rest of your code clearly assumes it's a newline separated text file. Personally, I would have used the plist format (it obviates the need to trim the whitespace from the individual words), but you can use whichever you prefer. But use one or the other, but not both.
If you're staying with the newline separated list of bad words, then just get rid of that line that says initWithContentsOfFile, you disregard the results of that, anyway. Thus:
- (void)getTheBadWordsAndSaveForLater {
// these should be local variables, so get rid of your instance variables of the same name
NSString *badWordsFilePath = [[NSBundle mainBundle] pathForResource:#"badwords" ofType:#"txt"];
NSString *badWordFile = [[NSString alloc] initWithContentsOfFile:badWordsFilePath encoding:NSUTF8StringEncoding error:nil];
// calculate `badwords` solely from `componentsSeparatedByString`, not `initWithContentsOfFile`
badwords = [badWordFile componentsSeparatedByString:#"\n"];
// confirm what we got
NSLog(#"Found %i words: %#", [badwords count], badwords);
}
You might want to look for whole word occurrences only, rather than just the presence of the bad word anywhere:
- (NSString *) removeBadWords:(NSString *) string {
NSLog(#"checking: %# for occurrences of these bad words: %#", string, badwords);
for (NSString* badword in badwords) {
NSString *searchString = [NSString stringWithFormat:#"\\b%#\\b", badword];
string = [string stringByReplacingOccurrencesOfString:searchString
withString:#"-"
options:NSCaseInsensitiveSearch | NSRegularExpressionSearch
range:NSMakeRange(0, string.length)];
}
NSLog(#"resulted in: %#", string);
return string;
}
This uses a "regular expression" search, where \b stands for "a boundary between words". Thus, \bhell\b (or, because backslashes have to be quoted in a NSString literal, that's #"\\bhell\\b") will search for the word "hell" that is a separate word, but won't match "hello", for example.
Note, above, I am also logging badwords to see if that variable was reset somehow. That's the only thing that would make sense given the symptoms you describe, namely that the loading of the bad words from the text file works but replace process fails. So examine badwords before you replace and make sure it's still set properly.

Parser over txt document in xcode

I would like to go through .txt document and store text blocks in NSStrings. My problem is that this document contains linebreaks, and i don't know how to rid of those. It would be nice, if i could put each individual word into an ordered NSArray and then just go through that array and get information out from that. I would something like this:
// txt file
This is just a test.
End of the text file.
// NSArray and NSStrings
NSArray *wholeDocument =#"This","is","just","a","test","Foo","bar.", "End", "of", "the","text","file.";
NSString *beginDocument =#"This is just a test";
NSString *endDocument =#"End of the text file.";
Try this:
NSString *str = [NSString stringWithContentsOfFile:#"file.txt"];
NSArray *arr = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Here, arr will contain all the words which are separated either by spaces or by line breaks.

Save NSArray variable as CSV file with objective-C

Please may i know how to save a NSArray variable as CSV file using objective-C and Xcode.
I want to save it in: \tmp file
NSArray *TArray;
NSMutableArray *Xdata;
NSMutableArray *Ydata;
for(int i = 1; i < 10; i++)
{
[Xdata addObject:[NSNumber numberWithFloat:(i/3.14)]];
[Ydata addObject:[NSNumber numberWithFloat:(i*3.14)]];
}
TArray = [[NSArray alloc] initWithObjects:Xdata, Ydata, nil];
So how can i save "TArray" as CSV file ?
Thanks :)
I think storing CSV file in NSArray does not mean anything... as CSV file will contain strings only. You can store CSV file in NSString.
Please go through my answer in the previous post for storing csvString in NSString.
EDIT:
I forgot to link my question, but please go through this link, which will give you example...
Generate CSV sample iphone app/code

How to properly create an array of arrays by reading in data from a file

So far I have:
//read in data from CSV file and separate it into 'row' strings:
// where dataString is simply a CSV file with lines of CSV data
// 31 lines with 9 integers in each line
NSArray *containerArray = [dataString componentsSeparatedByString:#"\n"];
NSArray *rowTemp; //local variable just for my sake
NSMutableArray *tableArray;//mutable array to hold the row arrays
//For each index of containerArray:
//take the string object (string of CSV data) and then,
//create an array of strings to be added into the final tableArray
for (int i = 0; i < [containerArray count]; i++) {
rowTemp = [[containerArray objectAtIndex:i] componentsSeparatedByString:#","];
[tableArray addObject:rowTemp];
}
Then when I try the following, it returns: (null)
NSLog(#"Row 6, cell 1 is %#", [[tableArray objectAtIndex:5] objectAtIndex:0]);
Any ideas? is there a better way?
FYI this data is static and very unlikely to change. Any way to create and populate a static array rather than using a mutable array, would be much appreciated.
Thanks in advance.
I just got the answer! with some help from iPhoneSDK forums, and what I failed to do was actually create the tableArray. What I did in the code above was merely create the variable. NSMutableArray *tableArray; and did not actually create the mutable array I wanted. Instead what I should have done was: NSMutableArray *tableArray = [NSMutableArray arrayWithCapacity: [containerArray count]]; Which worked like a charm.

Why is it that every other object in my array is blank?

I read a CSV file into an array using:
NSString *theWholeTable = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"example" ofType:#"csv"]
encoding:NSUTF8StringEncoding
error:NULL];
NSArray *tableRows = [theWholeTable componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
And every second object in the array is empty, any idea why?
the CSV file data looks like this:
10,156,326,614,1261,1890,3639,5800,10253,20914
20,107,224,422,867,1299,2501,3986,7047,14374
with 10 and 20 being the start of each new line.
thanks in advance.
Edit
I tried using the following code instead:
NSArray *tableRows = [theWholeTable componentsSeparatedByString:#"\n"];
And that worked the way I wanted it too.
Although I am still unsure why the newlineCharacterSet created empty objects...
If your CSV file comes from a non-UNIX system, it may contain multiple line separators (e.g. \r\n instead of \n). In this case, componentsSeparatedByCharactersInSet will insert empty strings for empty character sequences between \r and \n.
You can remove empty strings from NSArray using this method:
tableRows = [tableRows filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"length > 0"]];
to solve this problem, I have used other way:
NSArray *tableRows = [theWholeTable componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"\n"]]
So you wouldn't need remove any lines.