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.
Related
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:#","];
i have a text that contains end of lines; i would like to have that text introduced into a NSString and still recognize the end of line.
i.e. i don't want to have to place a "\n" at the end of every line.
how can i do so in Obj-c?
I think it's best to hold the text using an NSArray, each element of which is a separate line. You can use [NSString componentsSeparatedByCharactersInSet:] (reference) for that:
NSString *str = #"hello\nworld";
NSArray *lines = [str componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
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.
Well I've looked at similar problems over the site but haven't reached a solution thus far so I must be doing something wrong.
Essentially, I am importing a text file, then splitting each line into an element of an array. Since the text file will be updated etc.. I won't every know the exact amount of lines in the file and therefore how many elements in the array. I know in Java you can do .length() etc.. and supposedly in Objective C you can use 'count' but i'm having no luck returning the length of my array... suggestions?
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"allshows"
ofType:#"txt"];
NSString *fileString = [NSString stringWithContentsOfFile:filePath];
NSArray *lines = [fileString componentsSeparatedByString:#"\n"];
NSUInteger *elements = [lines count];
NSLog(#"Number of Shows : ", elements);
and what is being output is NOTHING. as in "Number of Shows : " - blank, like it didn't even count at all.
Thank you for any help!
You're missing the format string placeholder. It should be:
NSLog(#"Number of shows: %lu", elements);
You need to use a format specifier to print an integer (%d):
NSLog(#"Number of Shows : %d", elements);
Looking at your other post, it seems like you are a Java developer. In Java's System.out, you just append the variables. In Objective-C, I suggest you look at "print format specifiers". Objective-C uses the same format.
I have a file, which I'm reading into an NSString object using stringWithContentsOfFile. It contains Unicode for Japanese characters such as:
\u305b\u3044\u3075\u304f
which I believe is
せいふく
I would like my NSString object to store the string as the latter, but it is storing it as the former.
The thing I don't quite understand is that when I do this:
NSString *myString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
It stores it as: \u305b\u3044\u3075\u304f.
But when I hardcode in the string:
NSString *myString = #"\u305b\u3044\u3075\u304f";
It correctly converts it and stores it as: せいふく
Does stringWIthContentsOfFile escape the Unicode in some way? Any help will be appreciated.
Thanks.
In the file \u305b\u3044\u3075\u304f are just normal characters. So you are getting them in string. You need to save actual Japanese characters in the file. That is, store せいふく in file and that will be loaded in the string.
You can try this, dont know how feasible it is..
NSArray *unicodeArray = [stringFromFile componentsSeparatedByString:#"\\u"];
NSMutableString *finalString = [[NSMutableString alloc] initWithString:#""];
for (NSString *unicodeString in unicodeArray) {
if (![unicodeString isEqualToString:#""]) {
unichar codeValue;
[[NSScanner scannerWithString:unicodeString] scanHexInt:&codeValue];
NSString* betaString = [NSString stringWithCharacters:&codeValue length:1];
[finalString appendString:betaString];
}
}
//finalString should have せいふく
Something like \u305b in an Objective-C string is in fact an instruction to the compiler to replace it with the actual UTF-8 byte sequence for that character. The method reading the file is not a compiler, and only reads the bytes it finds. So to get that character (officially called "code point"), your file must contain the actual UTF-8 byte sequence for that character, and not the symbolic representation \u305b.
It's a bit like \x43. This is, in your source code, four characters, but it is replaced by one byte with value 0x43. So if you write #"\x43" to a file, the file will not contain the four characters '\', 'x', '4', '3', it will contain the single character 'C' (which has ASCII value 0x43).