각 표준 템플릿이있는 단일 .xml 파일을 만들려고 할 때마다 몇 가지 주요 비트의 데이터 만 변경됩니다. 조금 혼란 스럽지만 할 수 있습니다.Python 3.xx에서 여러 파일 만들기
자동화하려는 구조에는이 스크립트를 실행하고 사용자가 정의한 파일 번호 1, 10, 50, 100 등을 생성하는 폴더가 있습니다 (각 파일은 file_001.xml file_002입니다). .xml file_003.xml 등등. 이제 내가 할 수있는 지점에 이르렀지만 새 파일을 원할 때마다 스크립트를 실행해야합니다.
나는 눈부신 명백한 뭔가를 놓치고 있다고 느낍니다.
코드는 다음과 같습니다.
import random
import time
import glob
import os
import csv
# Importing random names & Sample types
with open('names_m.csv', 'r')as f:
reader = csv.reader(f)
male = list(reader)
with open('names_f.csv', 'r')as f:
reader = csv.reader(f)
female = list(reader)
with open('surnames.csv', 'r')as f:
reader = csv.reader(f)
surname = list(reader)
with open('sampletype.csv', 'r')as f:
reader = csv.reader(f)
stype = list(reader)
# getting today's date to put into line 4,5,6
date = time.strftime("%Y%m%d")
# string_1 Unique reference number
string_1 = random.randrange(1000000000, 9999999999)
# string_2 patient ID number
string_2 = 'P999990'
# string_3 ward selection
string_3 = random.choice(['W1', 'W2', 'F1', 'F2'])
# string_4 date + order number (string_5)
string_4 = date
# string_5 sample order number.
string_5 = random.randrange(00000000, 99999999)
string_6 = random.choice(surname)
string_7 = random.choice(male)
string_9 = random.choice(['M', 'F'])
# string_8 sample type.
string_8 = random.choice(stype)
# HL7 Message.
line1 = "MSH|^~\&|RHM||||||201702141105||ORM^O01|%s|P|2.5||NE|AL|||| \n" % (string_1)
line2 = "PID|1||%s^^^^HOSPITALNO~^^^^NHSNO||%s^%s||190701190000|%s|||||||||||||| \n" % (string_2, string_6, string_7, string_9)
line3 = "PV1|1||%s|||||||||||||||||||||||||||||||||||||||||||||||| \n" % (string_3)
line4 = "ORC|NW|%s%s||%s|||1^^^201702144500^^R||^^^20170214104500^^^^|||Test001||||REASON||||\n" % (string_4, string_5, string_5)
line5 = "OBR|1|%s%s||%s|||2017021411045|201702141045||Test001||||||||||\n" % (string_4, string_5, string_8)
line6 = "OBX|1|ST|%s%s||20170214%s|||||||||||||||\n" % (string_4, string_5, string_5)
line7 = "SPM|1|||||||||||||||||||||||||||||\n"
"""
# How many new files we want creating.
filecopy = input("How many files are to be created?:")
files = filecopy
"""
filecopy = 100
i = 1
while os.path.exists("S360_%s.xml" % i):
i += 1
if i == filecopy:
f.close()
else:
f = open('S360_%s.xml' % i, "w")
f.write(line1 + line2 + line3 + line4 + line5 + line6 + line7)
누군가 해결책을 갖고 있다면, 나는 모든 귀입니다. 또한
, 여기에 보너스 문제로 이미
files = [1] = +1
for files in files:
with open('S360_{}.xml'.format(files), "w") as f:
f.write(line1 + line2 + line3 + line4 + line5 + line6 + line7)
및
os.chdir("C:\\UAT DATA")
for file in glob.glob("*.xml"):
f = open((file.rsplit(".", 1)[0])+"xml", "w")
f.write(line1 + line2 + line3 + line4 + line5 + line6 + line7)
f.close()
그리고 마지막으로 일을 시도 일부 솔루션은 사람이 내가로부터 데이터를 인쇄하는 방법 경우에 빛을 창고 수 있습니다 쿠퍼 레이몬드 (Cooper Raymond)를 표시하는 것을 선호하는 이드. [ 'Cooper'] [ 'Raymond']와 같은 내 .csv 파일.
with open('names_m.csv', 'r')as f:
for line in f:
line.strip()
male = list(f)
감사보고 다른 사람을 위해 (나는이 CSV 부분 :)에 대한 수정 프로그램을 찾을 수 없음).
함수에서 하나의 파일을 만들기위한 코드를 캡슐화하십시오. 이 함수는 파일에서 상수가 아닌 모든 것을 입력으로 받아 들여야하며 모든 파일은 파일마다 다를 수 있습니다. 그런 다음 반복 할 때마다 파일을 만드는 함수를 호출하는 루프를 작성하면됩니다. –
@PatrickHaugh 귀하의 회신에 감사 드리며, 무지로 용서해주십시오. 그러나 나는 당신이 무슨 뜻인지 전혀 모르겠습니다. 당신은 내 첫 번째 코드 블록 52 & 57에서 f = open ("S360 _"(% s) ". xml"% i, "w")을 시도해야한다고 말합니까? – Lloyd