簡體   English   中英

如何將換行符插入列表列表

[英]How to insert the new line character into a list of lists

我正在嘗試在郵件標簽的名稱和地址部分之間插入換行符。 因為它是列表列表,所以不允許我在mailingLabelList的各部分之間輸入\\ n。

def main():
    """ Open's file, reads customer information into a list, closes the file"""
    custFile = open('customerData.txt','r')
    mailingLabelFile = open('mailingLabels.txt','w')
    customerList = generateList(custFile)
    mailingLabelList = generateMailingLabel(customerList)
    mailingLabelFile.write("mailingLabels")
    mailingLabelFile.close()
    mailingLabelFile = open('mailingLabels.txt','r')

    # Echo first and last enter from the customerList
    print "customerList[0]:", customerList[0]
    print "customerList[-1]:",customerList[-1]

    print mailingLabelList[0]

    custFile.close()
    mailingLabelFile.close()

def generateList(custFile):
    """ Reads customer data from file and returns a list of customers"""
    customers = []
    for line in custFile:
        # Strip the new-line character from the end of the line, then split
        # the line on the commas (',') to get a list of customer fields
        custInfoList = line.strip().split(',')
        customers.append(custInfoList)
    return customers

def generateMailingLabel(customerList):
    """Reads customer data from custoemrList and sorts out the female customers in Iowa
    and creates a mailing label for those customers"""
    mailingLabelList = []
    for list in customerList:
        if list[5] == 'IA' and list[10] == 'female':
            mailingLabelList.append(list[0] + ' ' + list[2] + ' ' + list[3] + ' ' + list[4] + ' ' + list[5] + ' ' + list[6])
        return mailingLabelList

main()
def generateMailingLabel(customerList):
    """Reads customer data from custoemrList and sorts out the female customers in Iowa
    and creates a mailing label for those customers"""
    mailingLabelList = []
    for list in customerList:
        if list[5] == 'IA' and list[10] == 'female':
            thing_to_append = ''.join(str(s) for s in list) + "\n"
            mailingLabelList.append(thing_to_append)
        return mailingLabelList

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM