0

My code is like this .

inputfile=np.genfromtxt('test1.dat')
for data in inputfile:
    lat=floor(data)+(floor(abs((data-floor(data))*100))/60)+....
    print lat

In command window I can see

12.9579738889
12.9579736111
12.9579727778
12.9579719444
12.9579711111
12.9579702778
12.9579694444
.......

But I want to save it in a text file in my working directory . I am not getting how to proceed. All attempts failed. Please give suggestions. Thanks in advance.

Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
Poka
  • 387
  • 1
  • 12
  • 1
    Please format the question properly ! – Murali Jun 22 '17 at 11:17
  • Please show at least a few of *all attempts* and the problems/errors that occurred. – Christian König Jun 22 '17 at 11:18
  • Just try open a new file using fopen and write the output. – Murali Jun 22 '17 at 11:19
  • Yeah new to python and not used this site so format is not proper . – Poka Jun 22 '17 at 11:23
  • inputfile=np.genfromtxt('test1.dat') x=[] #lat=[] for data in inputfile: lat=floor(data)+(floor(abs((data-floor(data))*100))/60)+(floor(((abs((data-floor(data))*100))-int((abs((data-floor(data))*100))))*100)/3600)+(((((abs((data-floor(data))*100))-int((abs((data-floor(data))*100))))*100)-floor(int((((abs((data-floor(data))*100))-int((abs((data-floor(data))*100))))*100))))*1000)/3600000 x.append(lat) print lat np.savetxt('output1.dat','w','lat') – Poka Jun 22 '17 at 11:24

2 Answers2

0

It's really simple. Use open to open the file (get a file object which encapsulates the file descriptor) and then simply write to that file. In a true Pythonic way you'll want to use a context manager (with open(..) as file), so the the file is closed automatically when out of context.

inputfile=np.genfromtxt('test1.dat')
with open("/path/to/output.file", "w") as f:
    for data in inputfile:
        lat=floor(data)+(floor(abs((data-floor(data))*100))/60)+....
        f.write("%f\n" % lat)
randomir
  • 17,989
  • 1
  • 40
  • 55
-1

You can do following. I used f-string to format output, which is available in Python >= 3.6, but you can you any version to do some calculation first and then output the value.

>>> with open('test1.dat') as f_in:
...     with open('outputfile.txt', 'w') as f_out:
...         for data in f_in:
...             f_out.write(f"{floor(data)+(floor(abs((data-floor(data))*100))/60)}\n")

if your data has more than one values you can use split function:

lan, lot = (float(x) for x in data.split(' '))

where ' ' is your separator between those values

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • Cool new feature. Although isn't `data` supposed to be `line` in your example? – kabanus Jun 22 '17 at 11:56
  • On the same note I want to ask one more question . Actually I have two columns of data latitude and longitude in dms.milliseconds format . I am converting it in degree through above equation separately . Is it possible to iterate both lat and lon columns from same file and do manipulation and again save to previous file. I am plotting them in google map – Poka Jun 22 '17 at 11:56
  • You probably should use ``float(line)`` instead of ``data`` (see [numpy.genfromtxt](https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html) docs) – randomir Jun 22 '17 at 12:11
  • @kabanus, yes, it should be data. I change my code. – Vlad Bezden Jun 22 '17 at 12:16
  • Dear Vlad Bezden Sir, Is it possible to iterate both lat and lon columns from same file in two variabls using previous equation and save them in another file. Now I am doing it two times for latitude and longitude – Poka Jun 22 '17 at 12:21
  • @SatinathDebnath, yes, I added more information to the example above. – Vlad Bezden Jun 22 '17 at 12:37
  • Sir, I am using dat file without any comma between two data . Only white space is present between columns. – Poka Jun 22 '17 at 13:16
  • @SatinathDebnath then use data.split(' ') (space) instead of ';' – Vlad Bezden Jun 22 '17 at 13:48
  • for value in inputfile: lan, data = value.split('') latitude=floor(data)+... f.write("%.8f\n" % latitude) #error:numpy.ndarray' object has no attribute 'split' – Poka Jun 22 '17 at 16:28
  • @SatinathDebnath use lan, lat = data.split(' ') – Vlad Bezden Jun 22 '17 at 17:42
  • @VladBezden, your code is still incorrect - it produces ``TypeError: a float is required``, since the ``data`` variable contains a string, and the ``floor`` function requires ``float``. See my previous comment. – randomir Jun 22 '17 at 19:22
  • @randomir I updated code to convert it to float. – Vlad Bezden Jun 22 '17 at 19:40
  • @VladBezden ,I have modified code as you suggested , But this time it is throws numpy.ndarray' object has no attribute 'split' error. Is it because I am loading file though np.genfromtxt() command? – Poka Jun 23 '17 at 02:12
  • for mata in inputfile: lan, data = (float(x) for x in mata.split(' ')) latitude=floor(data)+(floor(abs((data-floor(data))*100) +... f.write("%.8f\n" % latitude) – Poka Jun 23 '17 at 02:14
  • @VladBezden Sir, Would u please post your final code once again in steps. I am not getting using split function. Somewhere I am doing mistakes – Poka Jun 23 '17 at 13:03