<p>这应该让你接近,但我没有一堆明星身份证,也不是在我的领域。你知道吗</p>
<p>要点:</p>
<ol>
<li>使用<a href="https://docs.python.org/3/library/csv.html" rel="nofollow noreferrer">^{<cd1>}</a>模块读写文件。你知道吗</li>
<li>当您有需要多次调用的代码时(对于逻辑分组,通常甚至只调用一次),您需要考虑将其打包成<a href="https://www.datacamp.com/community/tutorials/functions-python-tutorial" rel="nofollow noreferrer">function</a></li>
</ol>
<p>你还有其他的研究方向。如果我不尝试使事情比基本循环更简洁一点,那么代码将相当长,我尽量不使它太简洁。希望你能继续跟进。你知道吗</p>
<pre><code>from lightkurve import search_lightcurvefile
import csv
# You need to read the file and get the star IDs. I'm taking a guess here that
# the file has a single column of IDs
with open('starID.txt') as infile:
reader = csv.reader(infile)
# Below is where my guess matters. A "list comprehension" assuming a single
# column so I just take the first value of each row.
star_ids = [item[0] for item in reader]
def data_reader(star_id):
"""
Function to read periodogram for a star_id
Returns [star_id, periodogram]
"""
lcf = search_lightcurvefile('201691589').download()
lc = lcf.PDCSAP_FLUX.remove_nans()
pg = lc.to_periodogram()
Prot= pg.frequency_at_max_power**-1
return [star_id, Prot]
# Now start calling the function on your list of star IDs and storing the result
results = []
for id_number in star_ids:
individual_result = data_reader(id_number) # Call function
results.append(individual_result) # Add function response to the result collection
# Now write the data out
with open('star_results.csv', 'w', newline='') as outfile:
writer = csv.writer(outfile)
writer.writerows(results)
</code></pre>