This is the code I'm using:
import pandas as pd
import os
out = 'E:/my_projects/SWAT_Projects/NLCD2006/OriginalModels/DL_corrected2NotCopy/FutureClimate/'
devils_p = open(os.path.join(out, 'Devils_P.txt')).readlines()
FutureClimate = pd.read_csv(os.path.join(out, 'EDMA_1_rcp26_2025_1.dat'), delimiter = '\t')
headers = [' ', 'year', 'day', 'tmin', 'tmax', 'pcp']
FutureClimate.columns = headers
df = FutureClimate.drop(FutureClimate.columns[0], axis = 1)
precip = pd.Series.tolist(df.pcp)
for i in precip:
devils_p.append('%s\n' % i)
thefile = open(os.path.join(out, 'weather.txt'), 'w')
for i in devils_p:
thefile.write(i)
Final length of devils_p
after appending precip
is 33237, however when I write it to a .txt file, the output length is smaller: around 32400. What could be a reason why the devils_p
is not written correctly?
EDIT1: if I reduce a number of items by 1000 in devils_p
list before appending precip
, everything is working fine. So, probably, the problem is related to maximum length of a list/.txt file?
EDIT2: previous edit was wrong. After I reduced length of an original list by 3652 items, the output .txt file is still not correct (ends earlier than it should be).
So, I have no idea why this error was happening, I assume something wrong was with my writing code part, but I solved my problem by using savetxt
from numpy
:
np.savetxt(os.path.join(out, 'weather.txt'), devils_p)