简体   繁体   English

Python:用不同的数字替换数组中的每个数字

[英]Python: Replacing every number in an array with a different number

I have data that is in the below format (in an ASCII file): 我有以下格式的数据(在ASCII文件中):

363 28 24 94 363 28 24 94

536 28 24 95 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. 例如,时间28对应于2015-02-01,纬度24对应于真实纬度-67等。

There are many different coded values. 有许多不同的编码值。 Time ranges from 0-28, lat from 0-24 and long from 0-108. 时间范围是0-28,lat是0-24和long是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. 我还要提到的是,上面显示的原始格式的输入文件长为79025行,对于每一行,我必须替换3个值。

This is my current attempt which is not working with error message: AttributeError: 'numpy.ndarray' object has no attribute 'replace' 这是我当前的尝试,不与错误消息一起使用:AttributeError:'numpy.ndarray'对象没有属性'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. 如有任何建议,我们将不胜感激。

Your codes look like indices, so you could use some numpy index tricks to get your result: 您的代码看起来像是索引,因此您可以使用一些numpy索引技巧来获得结果:

# 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]

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM