python从多个文本文件创建列表,并从这些列表创建csv文件

时间:2014-10-21 04:20:47

标签: python list csv text merge

运行到一个树桩,我有两个遗留文本文件,我想从中提取数据,以创建一个csv文件。

这里要做的就是我的代码与我在屏幕上完全一样:

import csv, itertools

list1 = []
with open('D:/py_files/legacy_temp/REPORT_1.TXT', 'rb') as tf:
    for line in tf:
        if len(line) > 2:
            if line[17].isdigit():
                acctnum = str(line[16:31])
                custname = str(line[39:58])
                currbal = str(line[84:96])
                diffbal = str(line[102:114])
                list1.append(acctnum + '|' + custname + '|' + currbal + '|' + diffbal)

list2 = []
with open('D:/py_files/legacy_temp/REPORT_2.TXT', 'rb') as tf2:
    for line in tf2:
        if line[0].isdigit():
            acctnum = str(line[1:12])
            ourbal = str(line[80:90])
            alldnum = str(line[123:131])
            clntnum = str(line[132:152])
            list2.append(acctnum + '|' + ourbal + '|' + alldnum + '|' + clntnum)

下面的代码只是我的剪贴簿,我正在尝试的事情。我可以创建csv文件,但它要么写为一个长连续行,要么在附加'|'时写入在每个char之后,即:a | b | c | d |等...

#mlist = []
#if len(list1) == len(list2):
#   for i, j in map(None,list1,list2):
#       print i + '|' + j
def f1():
    clist = []
    outfile = csv.writer(open('D:/py_files/legacy_temp_/report_diff.csv', 'wb'))
    if len(list1) == len(list2):
        for i, j in map(None,list1,list2):
            clist.append(str(i + '|' + j + '\n'))
        outfile.writerow(clist)
        print '\n'.join(clist)

def f2():
    for x,y in zip(list1,list2):
        print list1+list2
def f3():
    output = list(itertools.chain(list1,list2))
    print '\n'.join(output)

两件事,a)我是以正确的方式(分别打开两个文本文件),以及b)如果我是,我怎么能写一个csv文件,它会给我以下几行:

acctnum|custname|currbal|diffbal|acctnum|ourbal|alldnum|clntnum

上面|内的每个元素,在一个单独的单元格中。

PS。我只使用管道作为分隔符,因为余额中有逗号。我不需要使用管道,因为我可以替换余额中的逗号。

非常感谢所有帮助,谢谢

4 个答案:

答案 0 :(得分:1)

实际上,原始函数可以使用第二个函数进行一些小修改:

def f2():
    for x,y in zip(list1,list2):
        print list1+list2 <-- this should be print x+y

答案 1 :(得分:0)

应该把你要添加的内容放在括号中。

list1.append([acctnum + '|' + custname + '|' + currbal + '|' + diffbal])

您也可以这样做:

list1.append(['|'.join([acctnum, custname, currbal, diffbal])])

然后,您将获得list1中的一堆列表,这些列表代表一行。

答案 2 :(得分:0)

如果您想要以最快,最简单的方式将数据从txt转换为csv,您可以执行以下操作:

import csv

header = ('acctnum,custname,currbal,diffbal,acctnum,ourbal,alldnum,clntnum\n')
with open('out.csv', 'wb') as fout:
    fout.write(header)

    with open('blah1.txt', 'rU') as fin1:
        fin1.next()

        for row in fin1:
            fout.write(row.replace('|',','))

    fout.write('\n')

    with open('blah2.txt', 'rU') as fin2:
        fin2.next()

        for row in fin2:
            fout.write(row.replace('|',','))

这将获取您的两个文件,并在处理管道分隔符时将它们合并为一个CSV。如果你已经删除了你的管道,那么只需删除“.replace('|',',')位,这样你就只能将”row“传递给csv writer。然后你可以删除你没有的任何其他列。想要excel或者什么。

答案 3 :(得分:0)

谢谢,这是不正确的缩进。

import csv

path = 'D:/py_files/legacy_temp/'

outfile1 = csv.writer(open(path + 'REPORT_1.csv', 'wb'))
with open(path + 'REPORT_1.TXT', 'rb') as f1:
    for line in f1:
        lne = line.replace('\x0c','').replace('\x1c','').replace('\r','').replace('\n','')
        if len(lne) > 2:
            if lne[17].isdigit():
                list1 = []
                list1.append(str(lne[16:31].replace('-','').strip()))
                list1.append(str(lne[39:58].strip()))
                list1.append(str(lne[84:96].strip()))
                list1.append(str(lne[102:114].strip()))
                outfile1.writerow(list1)

outfile2 = csv.writer(open(path + 'REPORT_2.csv', 'wb'))
with open(path + 'REPORT_2.TXT', 'rb') as f2:
    for line in f2:
        lne = line.replace('\x0c','').replace('\x1c','').replace('\r','').replace('\n','')
        if len(lne) > 1:
            if lne[0].isdigit():
                list2 = []
                list2.append(str(lne[1:12].strip()))
                list2.append(str(lne[80:90].strip()))
                list2.append(str(lne[123:131].strip()))
                list2.append(str(lne[132:152].strip()))
                outfile2.writerow(list2)

现在我正在查看csv文件,我只是合并两个列表并创建一个csv文件。它们总是一样长。如果他们不是,那么报告有问题。我将开始研究这个......

修改: 这是合并......

import csv

path = 'D:/py_files/legacy_temp/'

with open(path + 'REPORT_MERGE.csv', 'wb') as csvf1:
    writer = csv.writer(csvf1)

    lst1 = []
    with open(path + 'REPORT_1.TXT', 'rb') as f1:
        for line in f1:
            lne = line.replace('\x0c','').replace('\x1c','').replace('\r','').replace('\n','')
            if len(lne) > 2:
                if lne[17].isdigit():
                    list1 = []
                    list1.append(str(lne[16:31].replace('-','').strip()))
                    list1.append(str(lne[39:58].strip()))
                    list1.append(str(lne[84:96].strip()))
                    list1.append(str(lne[102:114].strip()))
                    lst1.append(list1)

                    #creates ['x', 'x', 'x', 'x']

    lst2 = []
    with open(path + 'REPORT_2.TXT', 'rb') as f2:
        for line in f2:
            lne = line.replace('\x0c','').replace('\x1c','').replace('\r','').replace('\n','')
            if len(lne) > 1:
                if lne[0].isdigit():
                    list2 = []
                    list2.append(str(lne[1:12].strip()))
                    list2.append(str(lne[80:90].strip()))
                    list2.append(str(lne[123:131].strip()))
                    list2.append(str(lne[132:152].strip()))
                    lst2.append(list2)

                    #creates ['y', 'y', 'y', 'y']

    for x, y in zip(lst1,lst2):
        writer.writerow(x + y)
        #creates ['x', 'x', 'x', 'x', 'y', 'y', 'y', 'y']
        #each element in merged list writes to its own cell *****
相关问题