【发布时间】:2020-06-27 22:17:00
【问题描述】:
我正在编写一个循环来使用 os 包在 python 中生成文件。我的问题是每次生成文件时,我都需要检查文件内容是否满足要求,如果不满足则丢弃。我也不知道文件的名称,因为它由随机生成的名称组成,所以我需要另一种方法在我的代码中引用它。由于我有一个大型数据集,我不想手动执行此操作。
然后我的问题是如何引用刚刚作为 os 命令输出的文件?
如果有帮助,这是我的代码
# each row is for one patient
for row in range(2):
#for each id, find descriptors
local_id=df.loc[row,'# localid']
age=str(df.loc[row,'Age'])
if df.loc[row,'gender']=='Female':
gender='F'
else: gender='M'
indication=df.loc[row,'indication']
race=df.loc[row,'race']
#run loop to run until matching generated patient has matching race and indication
check=True
while check:
if indication=='Breast carcinoma':
os.system('run_synthea -p 1 -a '+age+' -g '+gender+' -m breast_cancer')
elif indication=='Hyperlipidemia':
os.system('run_synthea -p 1 -a '+age+' -g '+gender+' -m veteran_hyperlipidemia')
elif indication=='Colorectal Cancer / Polyps':
os.system('run_synthea -p 1 -a '+age+' -g '+gender+' -m colorectal_cancer')
else: #runs for , not selected, healthy, ovarian cancer, and heart diseases
os.system('run_synthea -p 1 -a '+age+' -g '+gender)
# INSET CODE , find output file
output_file=''
has_indication=check_indication(output_file, indication)
has_race=json.load(output_file)['entry'][0]['resource']['extension'][0]['extension'][1]['valueString']
if has_indication and has_race==race:
deID=output_file['entry'][0]['resource']['id']
thewriter.writerow([local_id,deID])
check=False
【问题讨论】:
-
如果文件写入当前工作目录,则新建一个目录,作为工作目录,然后搜索文件。
-
当
run_synthea运行时,它的输出是它用数据创建的文件的名称吗? -
您需要提供更多关于
run_synthea的详细信息。它的目的是什么,它是如何创建该文件的(根据什么规则)?它的输出是什么?您可以控制该应用程序吗? -
你知道输出目录吗?
-
我需要将
run_synthea更改为java -jar synthea-with-dependencies.jar,我还不能让它工作,所以我仍在努力。但是输出的是json格式的合成电子健康记录,其中文件名由随机生成的名称和ID组成,我知道输出目录。那么对于每个循环,我应该尝试引用当时创建的最新文件吗?
标签: python pandas dataframe operating-system