2017-05-31 106 views
0

我正在使用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和OBX都由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||||||||||||||||||||||||||||| 
+0

为什么所有的单独各行变量? –

+0

创建一行'列表';使用'random'模块 –

+0

@JonClements是的,第4,5,6行将被链接,但每行都是它自己的单独变量。我将用一块样本编辑 – Lloyd

回答

0

编辑链接

我现在看到您提供的样本数据,但我不知道这是所期望的您希望从get_lines方法得到的输出,还是您将要消耗的输入以产生来自get_lines的所需输出?


只需将想要打印的用户标识传递给变量即可。还可以使用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 

用户ID = [1,2,3,4,5,6,7,8 ,9]

+0

这是我当前从'get_lines'方法获得的输出。 – Lloyd

0

您可以喜欢这段代码。

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