gpt4 book ai didi

ios - 如何生成 CSV 文件?

转载 作者:可可西里 更新时间:2023-11-01 03:57:17 24 4
gpt4 key购买 nike

我将数据导出到 excel,它工作正常。但是我有一个小问题

我的输出是这样导出的:

enter image description here

我希望发生的事情是这样的:

enter image description here

这是我要导出的代码:

-(void)exportCSV {

NSArray * data = [NSArray arrayWithObjects:entries,keys, nil];
NSLog(@"%@",data);
csv =[NSMutableString string];
for (NSArray * line in data) {
NSMutableArray * formattedLine = [NSMutableArray array];
for ( field in line) {
BOOL shouldQuote = NO;
NSRange r = [field rangeOfString:@","];
//fields that contain a , must be quoted
if (r.location != NSNotFound) {
shouldQuote = YES;
}
r = [field rangeOfString:@"\""];
//fields that contain a " must have them escaped to "" and be quoted
if (r.location != NSNotFound) {
field = [field stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
shouldQuote = YES;
}
if (shouldQuote == YES) {
[formattedLine addObject:[NSString stringWithFormat:@"\"%@\"\"%@\"", entries,keys]];
} else {
[formattedLine addObject:field];
}
}

NSString * combinedLine = [formattedLine componentsJoinedByString:@";"];
[csv appendFormat:@"%@\n", combinedLine];

NSLog(@"%@",csv);
}

}

最佳答案

以下是否符合您的要求?
请注意,我没有考虑报价,我将其留给您;)
另请注意,我假设 entries.count == keys.count

- (void)exportCSV {    
NSArray *keys = @[@"T", @"R", @"RTT"];
NSArray *entries = @[@"-329180696", @"1243918297", @"-998693494"];
NSMutableString *csv = [[NSMutableString alloc] initWithCapacity:0];
for (int i = 0; i < entries.count; i++) {
[csv appendFormat:@"%@;%@\n", keys[i], entries[i]];
}
}

输出:

T;-329180696
R;1243918297
RTT;-998693494

关于ios - 如何生成 CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16038764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com