Based on this answer, I am using the changethis
method
import numpy as np
import os
def changethis(pos):
appex = sfile[pos[1]-1][:pos[2]] + '*' + file[pos[1]-1][pos[2]+len(pos[0]):]
file[pos[1]-1] = appex
pos = ('stack', 3, 16)
sfile = np.genfromtxt('in.cpp',dtype='str',delimiter=os.linesep)
changethis(pos)
print(file)
where the in.cpp
is a source file which contains the following:
/* Multi-line
comment
*/
#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, char *argv[]) {
int linecount = 0;
double array[1000], sum=0, median=0, add=0;
string filename;
if (argc <= 1)
{
cout << "Error" << endl;
return 0;
}
I get the output:
['using namespace std;' 'int main (int argc, char *argv[]) {'
'int linecount = *' 'double array[1000], sum=0, median=0, add=0;'
'string filename;' 'if (argc <= 1)' '{' 'cout << "Error" << endl;'
'return 0;' '}']
Notice that the lines of the multi-line comment, the include statements and the empty-lines are missing from the ndarray
.
I do not understand why this happens since the delimiter
is set to account for each change-of-line character.
Any suggestions on how the output to be:
['/* Multi-line' 'comment' '*/' '' '#include <iostream>',
'' '#include <fstream>' '' 'using namespace std;'
'' 'int main (int argc, char *argv[]) {'
'int linecount = *' 'double array[1000], sum=0, median=0, add=0;'
'string filename;' 'if (argc <= 1)' '{' 'cout << "Error" << endl;'
'return 0;' '}']