0

Matlab code

for i=1:N
        fprintf(fid,'%g 1 0 %g %g %g\n',i,coords(i,:));
    end
    fclose(fid);
end

Python code (converted by me)

for i in range ( 0 , N  ):
    fdata.write("%g 1 0 %g %g %g\n" % (coords(i,:)))

I got the following error.

File "<ipython-input-8-c5647c2ae12d>", line 44
    fdata.write("%g 1 0 %g %g %g\n" % (coords(i,:)))
                                                ^
SyntaxError: invalid syntax
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
amit
  • 1
  • 3

1 Answers1

0

You have to use a format string. You cannot write to file in python 3.x using that syntax. The syntax for writing to file is:

with open("file.txt", "w") as f:
    f.write(f"{variable}") # this is called a format string where the variable can be any datatype

If your intention is not to write to file then use print(f"{variable}") to print to your terminal under your for-loop.

These links might help you: Matlab to Python code link, tool to covert Matlab code to Python code and Learn format string.

Dharman
  • 30,962
  • 25
  • 85
  • 135
VRComp
  • 131
  • 1
  • 1
  • 12