将输出随机化为文件



i使用Python生成用于测试的数据。

我的整个过程几乎可以正常工作,但是,我有这个代码。

def get_lines():
    line1 = "Document Header - Once per document"
    line2 = "nDocument Information - Once per document"
    line3 = "nDocument Information 2 - Once per document"
    line4 = "nUser information 1"
    line5 = "nUser Information 1"
    line6 = "nUser Information 1"
    line7 = "nDocument Footer - Once per document"
    return line1 + line2 + line3 + line4 + line5 + line6 + line7

我想做的就是用用户信息填充LINE4,5,6 2,3,4类似的东西:

line1 = "Document Header - Once per document"
line2 = "nDocument Information - Once per document"
line3 = "nDocument Information 2 - Once per document"   
line4 = "nUser information 1"
line5 = "nUser Information 1"
line6 = "nUser Information 1"  
line4 = "nUser information 2"
line5 = "nUser Information 2"
line6 = "nUser Information 2"
line4 = "nUser information 3"
line5 = "nUser Information 3"
line6 = "nUser Information 3"
line7 = "nDocument Footer - Once per document"

,但要随机进行随机,即我要10个文件,有些文件将包含一块用户信息,其中2个三个3等...

...

我正在努力寻找一种一致的方法来生产我的需求。

谢谢。

编辑:添加的示例消息:ORC OBR和OFX均由UID的

链接
MSH|^~&||||||||201705301105||ORM^O01|4960855009|P|2.5||NE|AL||||
PID|1||^^^^HOSPITALNO~^^^^NHSNO||Hendry^John||190203130000|F|||||||||||||| 
PV1|1||G2D|||||||||||||||||||||||||||||||||||||||||||||||| 
ORC|NW|2017053019783377||19783377|||1^^^201705304500^^R||^^^201705
OBR|1|2017053019783377||1019|||2017053011045|201705301045||Test001||||||||||
OBX|1|ST|2017053019783377||2017053019783377|||||||||||||||
SPM|1|||||||||||||||||||||||||||||

编辑

我现在看到您提供了示例数据,但是我不确定这是您从get_lines方法期望的所需输出,还是您将要消费的输入从get_lines产生所需的输出?


只需通过要打印的用户ID传递变量即可。您也可以使用random.choice从列表中随机选择一个值,也可以使用random.randint

在范围内传递随机整数
def get_lines(userid):
    line1 = "Document Header - Once per document"
    line2 = "nDocument Information - Once per document"
    line3 = "nDocument Information 2 - Once per document"
    line4 = "nUser information {}".format(userid)
    line5 = "nUser Information {}".format(userid)
    line6 = "nUser Information {}".format(userid)
    line7 = "nDocument Footer - Once per document"
    return line1 + line2 + line3 + line4 + line5 + line6 + line7

userIds = [1,2,3,4,4,5,6,7,8,9]

您可以喜欢此代码。

def get_lines():
    line1 = "Document Header - Once per document"
    line2 = "nDocument Information - Once per document"
    line3 = "nDocument Information 2 - Once per document"
    line4 = "nUser information 1"
    line5 = "nUser Information 1"
    line6 = "nUser Information 1"
    line7 = "nDocument Footer - Once per document"
    return setLine(line1, 1) + setLine(line2, 1) + setLine(line3, 1) + setLine(line4, 3)+ setLine(line4, 3, "1", "2")

def setLine(content, iNum = 1, oldStr="", newStr=""):
    strStr = ""
    for ii in range(0, iNum):
    strStr += content.replace(oldStr, newStr)
    return strStr
print(get_lines())

样本代码输出是:

Document Header - Once per document
Document Information - Once per document
Document Information 2 - Once per document
User information 1
User information 1
User information 1
User information 2
User information 2
User information 2

相关内容

  • 没有找到相关文章

最新更新