我正在尝试将脚本和其他相关信息的源代码片段写入csv文件,其中每个单元格将包含一条信息
期望输出如下:
function name, number of lines, source code
helloWorld, 3, {
printf("hello, world\n");
}
fooBar, 5, {
const char *foo = "Hello";
const char *bar = "World!";
fprintf(stdout, "%s %s\n", foo, bar);
return 0;
}
其中,每个源代码片段应位于单个单元格中,同时保留代码结构
代码如下:
with open('functionInformation.csv', 'wb') as csvOut:
csvwriter = csv.writer(csvOut, delimiter = ',')
csvwriter.writerow(['function name', 'number of lines', 'source code'])
for functionObject in functionObjectRepository:
csvwriter.writerow([functionObject.funcName, functionObject.numLines, functionObject.sourceCode])
其中functionObject是具有函数名(funcName)、代码行数(numLines)和实际源代码(sourceCode)等属性的对象
我现在得到的输出如下所示:
helloWorld, 3, {
printf("hellp, world\n");
}
fooBar, ....
根据this site:
然后显示示例:
但是,将代码中的所有
\n
替换为\r\n
也会更改代码本身中的那些。所以也许你得找到更好的方法来改变相关问题 更多 >
编程相关推荐