我在 Flutter 应用程序中编写了几个函数来导出和导入数据以进行备份。 数据库中的部分数据包括音频剪辑的路径。 当我导出数据时,文件路径中的句号将转换为“\u002e”。 当我重新导入文件时,“\u002e”不会转换回句号。
出口线路为:
outFile.writeAsStringSync(exportFrequencies(), mode: FileMode.write, flush: true);
默认为 utf8 编码。 我也尝试过添加选项
encoding: const AsciiCodec()
,但这并没有产生什么影响。
导入线路为:
List<String> data = File(fullName).readAsLinesSync();
同样,默认为 utf8,我尝试过 ascii 编码,但这并没有什么区别。
我还尝试在数据导入行之后编写一些拼凑代码,以使用三种不同的方法自行更改代码,如下所示,但这并没有将“\u002e”代码更改回“。”任何一个。 第二行和第三行解码来自答案here。
for (String s in data) {
s.replaceAll('\u002e', '.'); // Only 1 of these decode lines included per run
s = utf8.decode(s.runes.toList()); // Only 1 of these decode lines included per run
s = utf8.decode(s.codeUnits); // Only 1 of these decode lines included per run
print('s: $s');
}
导入后和上述解码行后的输出始终是:
s: data:frequency,fid:1,iid:2,iname:bagodingledongs,rid:1,rname:chirrupsagogo,rtype:R,fstart:100,fend:999,fundamental:0,audiofile:mixkit-dog-barking-twice-1\u002ewav,audiopath:/data/user/0/com\u002eexample\u002efrequent/cache/file_picker/1735568632650/mixkit-dog-barking-twice-1\u002ewav
如您所见,使用任何导入/解码方法都没有更改代码。
我还在 VS Code 中询问了 CoPilot,他提供了:
data[i] = utf8.decode(data[i].codeUnits);
data[i] = data[i].replaceAll('\u002e', '.');
...还有...
data[i] = utf8.decode(data[i].codeUnits);
data[i] = data[i].replaceAll(r'\\u002e', '.');
这些都不起作用。
我觉得我在这里遗漏了一些关于如何处理 unicode 字符的明显内容。
如何导入数据并将“\u002e”代码转换回“.” (或者首先将它们导出为“.”)?
哦。
简单的答案是转义我的拼凑中的斜线,如下所示:
s.replaceAll('\\u002e', '.');
如果有一种方法可以作为导入线的一部分来做到这一点(
List<String> data = File(fullName).readAsLinesSync();
)我仍然有兴趣听到这一点。