问题
I have a .plist file containing array and dictionaries. I want to convert this into a CSV file so I can then upload this online to a database. Does anyone know of a simple way to do this?
回答1:
Dave DeLong has written a CSV parser which will help you accomplish this task, you can get more info on this answer he has posted in this question.. How to convert the NSMutableArray to CSV file on iPhone?
回答2:
Look the following code
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
NSArray *commentArray = [NSArray arrayWithContentsOfFile:plistPath];
NSMutableString *mainString=[[NSMutableString alloc]initWithString:@""];
for(int i=0;i<[commentArray count];i++ ) {
NSString *string=[[commentArray objectAtIndex:i]objectForKey:@"cmtName"];
string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
[mainString appendFormat:@"\"%@\"",string];
string=[[commentArray objectAtIndex:i]objectForKey:@"cmtDesc"];
string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
[mainString appendFormat:@",\"%@\"",string];
string=[[commentArray objectAtIndex:i]objectForKey:@"cmtType"];
string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
[mainString appendFormat:@",\"%@\"",string];
[mainString appendFormat:@",\"%@\"",string];
[mainString appendFormat:@"\n"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"Docment.conf"];
NSData* settingsData;
settingsData = [mainString dataUsingEncoding: NSASCIIStringEncoding];
if ([settingsData writeToFile:filePath atomically:YES])
NSLog(@"writeok");
You can also use https://github.com/davedelong/CHCSVParser for generating CSV.
来源:https://stackoverflow.com/questions/8396179/convert-plist-to-csv-file