0

I have new string from file ("new.dat") to be merge with file ("old.dat").

File : "new.dat"
WBGG 120200Z VRB03KT 9000 FEW015 BKN160 28/25 Q1013 NOSIG

File : "old.dat"
WBGG 120130Z VRB02KT 9000 FEW015 BKN150 27/25 Q1013 NOSIG    
WBGG 120100Z VRB02KT 9999 FEW014 BKN150 26/25 Q1012 NOSIG

The expected output will overwrite and became new ("old.dat")

WBGG 120200Z VRB03KT 9000 FEW015 BKN160 28/25 Q1013 NOSIG
WBGG 120130Z VRB02KT 9000 FEW015 BKN150 27/25 Q1013 NOSIG    
WBGG 120100Z VRB02KT 9999 FEW014 BKN150 26/25 Q1012 NOSIG

The process will continue as the new string in "new.dat" appeared in the system. The result from the current script is weird. Anyone got ideas?

new=open("new.dat","r")
old=open("old.dat","r")

for line1 in new:
    data1=line1

for line2 in old:
    data2=line2   

newdata=np.array([data1,data2])

## overwrite file and become old file
newfile = open("old.dat", 'w')
newfile.write(newdata)
newfile.close() 
6
  • If you are going to use np, you might aswell go for reading the files with np. Concatenate or vstack seems right way to go. Commented May 12, 2018 at 2:02
  • Please explain what is the "result from the current script" instead of just "weird". Commented May 12, 2018 at 2:05
  • Also please have python-3.x or python-2.7 too. Commented May 12, 2018 at 2:06
  • I used python-2.7...the weird output is ignoring line no. 2. WBGG 120130Z....is missing.. Commented May 12, 2018 at 2:11
  • it just saved line 1 and 3 only.. Commented May 12, 2018 at 2:12

2 Answers 2

1

This is what I meant with "sticking to numpy":

a = np.loadtxt('old.dat', dtype='object')
b = np.loadtxt('new.dat', dtype='object')

c = np.vstack((b,a))

np.savetxt('old.dat', c, delimiter=" ", fmt="%s")

Or you could use pandas:

import pandas as pd

(pd.concat((pd.read_csv(i, sep=' ', header=None) for i in ('new.dat', 'old.dat')))
    .to_csv('old.dat', sep=' ', index=False, header=None))

But I would probably use some shell script for this. Windows for instance (type this into terminal in the folder)

(type old.dat new.dat) >> old.dat
Sign up to request clarification or add additional context in comments.

8 Comments

...it woroks..great. I am so happy for that...yess!!...but how to make sure the recent new data is place on top??
@Azam You change the order of (a,b) to (b,a)
I did not know vstack is helpful in here...thanks again @Anton vBR
what if the data in different dimension or columns?. how to combine it..
the above example was in exact column hence no problem to use vstack...@Anton vBR
|
1

We can use append for writting new data to the old file ,as new data becomes old data for next incoming New data . Try to change

newfile = open("old.dat", 'w')

To

newfile = open("old.dat", 'a')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.