Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python从多个文本文件创建列表,并从这些列表创建csv文件_Python_List_Csv_Text_Merge - Fatal编程技术网

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

python从多个文本文件创建列表,并从这些列表创建csv文件,python,list,csv,text,merge,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():

遇到一个树桩,我有两个遗留文本文件,我想从中提取数据,以便创建一个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文件,但它要么作为一个长的连续行写入,要么在每个字符后面附加一个“|”,即: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
将上述
|
中的每个元素放在单独的单元格中

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


非常感谢您提供的所有帮助,谢谢

请将您想要添加的内容放在括号中

list1.append([acctnum + '|' + custname + '|' + currbal + '|' + diffbal])
你也可以这样做:

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

然后,您将在列表1中获得一组列表,这些列表表示一行。

如果您希望以最快、最简单的方式将数据从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编写器。然后,您可以删除excel或其他内容中不需要的任何其他列。

谢谢,这是不正确的缩进

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 *****

实际上,原始函数将与第二个函数配合使用,只需稍作修改:

def f2():
    for x,y in zip(list1,list2):
        print list1+list2 <-- this should be print x+y
def f2():
对于zip中的x,y(列表1,列表2):
打印列表1+列表2