Skip to content Skip to sidebar Skip to footer

Python Numpy Ndarray Skipping Lines From Text

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

Solution 1:

Again sorry for the use of genfromtxt, didn't understood your intentions, just tried to provide a possible solution for the problem. As a follow up for that particular solution (others have been provided) you can just do:

import numpy as np
import os

def changethis(pos):
    # Notice file is in global scope
    appex = file[pos[1]-1][:pos[2]] + '*' + file[pos[1]-1][pos[2]+len(pos[0]):]
    file[pos[1]-1] = appex

pos = ('stack', 3, 16)
file = np.array([i for i in open('in.txt','r')]) # instead of genfromtext.
changethis(pos)
print(file)

, which resulted in:

['/* Multi-line \n''comment\n''*/\n*''\n''#include <iostream>\n''#include <fstream>\n''\n''using namespace std;\n''\n''int main (int argc, char *argv[]) {\n''  int linecount = 0;\n''  double array[1000], sum=0, median=0, add=0;\n''  string filename;\n''  if (argc <= 1)\n''      {\n''          cout << "Error" << endl;\n''          return 0;\n''      }']

EDIT: Also another relevant point mentioned by another user is the scope I was using for file. I did not mean to tell you to do stuff in global scope, I meant to explain that the function was working because file was in global scope. In any case you can create a function to hold the scope:

import numpy as np
import os

defchangeallthese(poslist,path):
    defchangethis(pos):
        appex = file[pos[1]-1][:pos[2]-1] + '*' + file[pos[1]-1][pos[2]-1+len(pos[0]):]
        file[pos[1]-1] = appex
    file = np.array([str(i) for i inopen(path,'r')])
    for i in poslist:
        changethis(i)
    return file

poslist = [('stack', 3, 16),('stack', 18, 1),('/* Multi-line', 1, 1)]
file =   changeallthese(poslist,'in.txt')
print(file)

, which results in:

['* \n''comment\n''*/\n*''\n''#include <iostream>\n''#include <fstream>\n''\n''using namespace std;\n''\n''int main (int argc, char *argv[]) {\n''  int linecount = 0;\n''  double array[1000], sum=0, median=0, add=0;\n''  string filename;\n''  if (argc <= 1)\n''      {\n''          cout << "Error" << endl;\n''          return 0;\n''* }']

To write an array to file you can either use the normal file writing system in Python:

fid = open('out.txt','w')
fid.writelines(file)
fid.close()

, or use a function from numpy (but I'm not sure if it will add more endlines or not so be careful):

np.savetxt('out.txt',file,fmt='%s')

Solution 2:

If you want a list of strings representing the lines of a file, open the file and use readlines():

withopen('in.cpp') as f:
    lines = f.readlines()

# Have changethis take the list of lines as an argument
changethis(lines, pos)

Don't use np.genfromtxt; that's a tabular data parser with all sorts of behavior you don't want, such as treating # as a line comment marker.

Depending on what you intend to do with this list, you can probably even avoid needing an explicit list of lines. Also, file is a bad choice of variable name (it hides the built-in file), and changethis should really take the list as an argument instead of a global variable. In general, the earlier answer you got was pretty terrible.

Solution 3:

If the file is not too big:

import numpy as np
import os

defchangethis(linelist,pos):
    appex = linelist[pos[2]-1][:pos[3]] + pos[1] + linelist[pos[2]-1][pos[3]+len(pos[0]):]
    linelist[pos[2]-1] = appex

pos = ('Multi','Three', 1, 3)

withopen('in.cpp','r')  as f:
    lines=f.readlines()
    changethis(lines,pos)
print(''.join(lines))

readlines turns your file into a list of lines(which is memory-inefficient and slow, but does the job. If less than 1k lines it should be fine).

The function takes a list of lines as input, in addition to pos. I also modified the function to replce pos[0] with pos[1] instead of a * at line pos[2] and after character pos[3].

I get this as output:

/* Three-line 
comment
*/#include<iostream>#include<fstream>usingnamespace std;

intmain(int argc, char *argv[]){
  int linecount = 0;
  double array[1000], sum=0, median=0, add=0;
  string filename;
  if (argc <= 1)
      {
          cout << "Error" << endl;
          return0;
      }

Post a Comment for "Python Numpy Ndarray Skipping Lines From Text"