目录中有这个值:
0.6881 -55.0099
0.6883 -80.3465
0.7827 -59.5199
0.8206 -54.5565
0.8418 -49.7932
0.9503 -43.6182
1.0236 -52.9165
1.0488 -50.6465
1.1068 -50.6182
1.134 -32.2499
1.1325 -45.4482
1.2633 -27.7416
1.2725 -50.8282
1.3115 -40.6049
1.3461 -30.5649
...
...
我之前声明的值是ra = 59.6517601 dec = 61.5475502。如何比较显示我的目录中是否有值ra,dec?
答案 0 :(得分:2)
首先在python中创建目录:
l = '0.6881 -55.0099 0.6883 -80.3465 0.7827 -59.5199 0.8206 -54.5565 0.8418 -49.7932 0.9503 -43.6182 1.0236 -52.9165 1.0488 -50.6465 1.1068 -50.6182 1.134 -32.2499 1.1325 -45.4482 1.2633 -27.7416 1.2725 -50.8282 1.3115 -40.6049 1.3461 -30.5649'
l = l.replace(' ', ',')
l = l.split(',')
l = [float(a) for a in l]
ra = 59.6517601
dec = 61.5475502
ra in l
dec in l
两者都输出False,因此列表中既没有ra也没有dec。 想象一下ra在列表中,然后:
l.append(ra)
ra in l
True
答案 1 :(得分:2)
在此之前,如果const Root = ({ store }) => (
<Provider store={store}>
<Router>
<Route path="/" component={App} />
</Router>
</Provider>
)
和ra
的小数与csv文件中的值的小数不同,请先使用dec
来修复小数,然后执行以下操作< / p>
使用Pandas:
round()
没有熊猫:
import pandas
ra = 59.6517601
dec = 61.5475502
pattern = [ra,dec]
table = pandas.read_csv('file.csv')
for ind,row in table.iterrows():
if list(row.values)==pattern:
print('pattern was detected at index {}'.format(ind))
请记住,如果你在第二个解决方案中进入cnt = 0
ra = 59.6517601
dec = 61.5475502
pattern = [ra,dec]
with open('file.csv','r').read() as file:
for row in file.split('\n'):
x = [float(row.split(',')[0]),float(row.split(',')[1])]
if x==pattern:
print('pattern was detected at index {}'.format(cnt))
break
cnt += 1
,可能是因为文件中文本的ValueError
行为,其中在列表的最后一个索引处创建了一个空元素。要避免错误,请执行以下操作:
split
语法后的 file = file[:-1]
答案 2 :(得分:1)
感谢您帮助回答我的问题,对不起我的延迟回答!好吧,我找到了一个简单的解决方案。我正在使用拟合文件。
catalogue = pd.read_csv('cat7.csv', delimiter=(','))
catalogue.columns = ['ra', 'dec']
df1 = catalogue[['ra','dec']]
ra = []
dec = []
for r in df1['ra']:
ra.append(r)
for d in df1['dec']:
dec.append(d)
for filename in os.listdir('6df'):
print('reading file ' + filename)
if filename.endswith('.fits'):
hdulist = fits.open('6df/' + filename)
try:
ra1 = hdulist[2].header['ra']
dec1 = hdulist[2].header['dec']
for i in range(0, len(df1)):
if (int(ra1) == int(ra[i]) and int(dec1) == int(dec[i])):
shutil.copy('6df/' + filename, 'type/' + filename)
print(filename + ' copiado')
break
except:
shutil.copy('6df/' + filename, 'SEM_RA_DEC/' + filename)