1

I have data that is in the below format (in an ASCII file):

363 28 24 94

536 28 24 95

where we have speed, time, lat, long. Time, lat and long values are all codes for the true values. For example a time of 28 corresponds to 2015-02-01, lat of 24 corresponds to a true latitude of -67 etc.

There are many different coded values. Time ranges from 0-28, lat from 0-24 and long from 0-108.

I would like the replace every single one of the 'code' values with their true counterpart and output into a text file. The format of the text file would be speed, true time, true lat, true long.

I have tried doing this by using a dictionary and replace, however replace does not seem to like the fact that I am reading in an array.

I should also mention that the input file with the original format shown above is 79025 lines long and for each line I have to replace 3 values.

This is my current attempt which is not working with error message: AttributeError: 'numpy.ndarray' object has no attribute 'replace'

data=np.genfromtxt('./data/u_edit_2.coords')
time=data[:,[1]]
lat=data[:,[2]]
lon=data[:,[3]]
def replace_all(text, dic):
     for i, j in dic.iteritems():
         text = text.replace(i, j)
     return text
reps = {'0':'2015-01-02', '1':'23773', '2':'23774'}
time_new = replace_all(time, reps)
print time_new

Any suggestions would be appreciated, cheers.

1
  • If the codes translate to actual values through simple functions, that would be a more effective way to do the translation. How are the codes related to the values? Commented Dec 4, 2015 at 16:10

2 Answers 2

3

Your codes look like indices, so you could use some numpy index tricks to get your result:

# your values go here, where lat_values[24] = -67, etc.
time_values = np.array(['2015-01-01', '2015-01-02', ...])
lat_values = np.array([...])
lon_values = np.array([...])

# read the coded coords
_, time, lat, lon = np.loadtxt('coords', dtype=int).T

# decode
time = time_values[time]
lat = lat_values[lat]
lon = lon_values[lon]
Sign up to request clarification or add additional context in comments.

Comments

-1

This looks to be better handled as a file processing issue. You can then process the file once and read in the processed data whenever needed, without additional processing.

fmt = "{},{},{},{}\n"  #or whatever format you want
def transform(line):
  speed, time, lat, lon = line.strip().split()
  return fmt.format(
    speed,
    true_time(time),
    true_lat(lat),
    true_lon(lon)
  )

#change the next three functions to fit your needs
def true_time(time):
  return time
def true_lat(lat):
  return lat
def true_lon(lon):
  return lon

fin = open("c:/temp/temp.txt","r")
fout = open("c:/temp/temp2.txt","w")
for line in fin:
  if line.strip(): #ignore empty lines
    fout.write(transform(line))

fin.close()
fout.close()

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.