Search code examples
numpytxtcarriage-return

unexpected '\r' carriage return from np.loadtxt


I am reading a .dat file using the following command

accel_x = np.loadtxt('./GroundMotions/GM/%s_%s_%s_%s_x.dat' % (station, realization, soilpro, profile))

where station, realization, soilpro, and profile are all Strings.

Then python throws me an error:

FileNotFoundError: [Errno 2] No such file or directory: './GroundMotions/GM/Z0XOCS_csz002_C4_WA-DNR-08_14\r_x.dat'

There is an unexpected \r in the error message. How can I avoid this unexpected carriage return?


Solution

  • You could strip it:

    accel_x = np.loadtxt('./GroundMotions/GM/%s_%s_%s_%s_x.dat' % (station, realization, soilpro.strip('\r'), profile))
    

    Or to strip all parameters:

    './GroundMotions/GM/%s_%s_%s_%s_x.dat' % tuple(str.strip(s, '\r') for s in (station, realization, soilpro, profile))