Convert plist to CSV file?

空扰寡人 提交于 2019-12-14 00:28:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!