使用python读取txt文件的每一行并将其部分分成csv文件

时间:2018-12-30 22:28:19

标签: python csv data-conversion

我有一个txt文件,其格式设置为每行最多784个字符。每行都是csv中的一行,某些长度的字符是列。

我可以使其在第一行上完美运行,但是我无法弄清楚如何使其在文件的每一行上运行。我尝试了几种不同的方法,但是我认为我只是走错了兔子洞。希望大家能帮忙!代码如下:

with open('file.txt', 'r') as f, open('file.csv', 'w') as out_f:

        each_line = f.readline()
        filet = each_line[0]
        srcky = each_line[1: 33]
        clmst = each_line[34]
        postd = each_line[35:43]
        rcvdt = each_line[43:51]
        mbrno = each_line[51:62]
        pelcd = each_line[62:64]
        plname = each_line[64:89]
        pmfnam = each_line[89:114]
        pmbidt = each_line[114:122]
        pmbsex = each_line[122]
        mmbrno = each_line[123:134]
        mlname = each_line[134:159]
        mmfnam = each_line[159:184]
        mmbsex = each_line[184]
        mmbidt = each_line[185:193]
        grpid = each_line[193:199]
        plncd = each_line[199:202]
        aprno = each_line[202:211]
        prvno = each_line[211:217]
        psnam = each_line[217:232]
        ptype = each_line[232:242]
        pazip = each_line[242:251]
        pprov = each_line[251]
        lineCounter = each_line[252:260]
        ssvdt = each_line[260:268]
        ensvdt = each_line[268:276]
        enplsv = each_line[276:278]
        ensrsn = each_line[278:280]
        becat = each_line[280:283]
        diag1 = each_line[283:291]
        diag2 = each_line[291:299]
        diag3 = each_line[299:307]
        diag4 = each_line[307:315]
        diag5 = each_line[315:323]
        diag6 = each_line[323:331]
        diag7 = each_line[331:339]
        diag8 = each_line[339:347]
        diag9 = each_line[347:355]
        diag10 = each_line[355:363]
        endxf1 = each_line[363]
        endxf2 = each_line[364]
        endxf3 = each_line[365]
        endxf4 = each_line[366]
        pcdcd = each_line[367:376]
        emod1 = each_line[376:378]
        emod2 = each_line[378:380]
        emod3 = each_line[380:382]
        emod4 = each_line[382:384]
        pcdqt = each_line[384:387]
        pcdqt1 = each_line[387:389]
        bilam = each_line[389:400]
        netam = each_line[400:411]
        alwam = each_line[411:422]
        dctam = each_line[422:433]
        copam = each_line[433:444]
        ncvam = each_line[444:455]
        cobsv = each_line[455:466]
        ncrsn = each_line[466:468]
        revcd = each_line[468:472]
        drgcd = each_line[472:475]
        sprc1 = each_line[475:483]
        sprc2 = each_line[483:491]
        sprc3 = each_line[491:499]
        sprc4 = each_line[499:507]
        sprc5 = each_line[507:515]
        sprc6 = each_line[515:523]
        filetc = each_line[523:570]
        vvndno = each_line[570:580]
        vname = each_line[580:610]
        vadd1 = each_line[610:665]
        vadd2 = each_line[665:720]
        vcity = each_line[720:735]
        vstate = each_line[735:737]
        vzip = each_line[737:746]
        hpatc = each_line[746:784]
        icdver = each_line[784]

        newfile = (filet + ',' + srcky + ',' + clmst + ',' + postd + ',' + rcvdt + ',' + mbrno + ',' + pelcd + ',' + plname + ',' + pmfnam + ',' + pmbidt + ',' + pmbsex + ',' + mmbrno + ',' + mlname + ',' + mmfnam + ',' + mmbsex + ',' + mmbidt + ',' + grpid + ',' + plncd + ',' + aprno + ',' + prvno + ',' + psnam + ',' + ptype + ',' + pazip + ',' + pprov + ',' + lineCounter + ',' + ssvdt + ',' + ensvdt + ',' + enplsv + ',' + ensrsn + ',' + becat + ',' + diag1 + ',' + diag2 + ',' + diag3 + ',' + diag4 + ',' + diag5 + ',' + diag6 + ',' + diag7 + ',' + diag8 +
                ',' + diag9 + ',' + diag10 + ',' + endxf1 + ',' + endxf2 + ',' + endxf3 + ',' + endxf4 + ',' + pcdcd + ',' + emod1 + ',' + emod2 + ',' + emod3 + ',' + emod4 + ',' + pcdqt + ',' + pcdqt1 + ',' + bilam + ',' + netam + ',' + alwam + ',' + dctam + ',' + copam + ',' + ncvam + ',' + cobsv + ',' + ncrsn + ',' + revcd + ',' + drgcd + ',' + sprc1 + ',' + sprc2 + ',' + sprc3 + ',' + sprc4 + ',' + sprc5 + ',' + sprc6 + ',' + filetc + ',' + vvndno + ',' + vname + ',' + vadd1 + ',' + vadd2 + ',' + vcity + ',' + vstate + ',' + vzip + ',' + hpatc + ',' + icdver)
        out_f.write(str(newfile))
      out_f.close

2 个答案:

答案 0 :(得分:0)

您需要使用for循环遍历输入文件中的每一行。另外,您无需在with语句内关闭文件句柄

with open('file.txt', 'r') as f, open('file.csv', 'w') as out_f:
    for each_line in f:
        filet = each_line[0]
        srcky = each_line[1: 33]
        clmst = each_line[34]
        postd = each_line[35:43]
        rcvdt = each_line[43:51]
        mbrno = each_line[51:62]
        pelcd = each_line[62:64]
        plname = each_line[64:89]
        pmfnam = each_line[89:114]
        pmbidt = each_line[114:122]
        pmbsex = each_line[122]
        mmbrno = each_line[123:134]
        mlname = each_line[134:159]
        mmfnam = each_line[159:184]
        mmbsex = each_line[184]
        mmbidt = each_line[185:193]
        grpid = each_line[193:199]
        plncd = each_line[199:202]
        aprno = each_line[202:211]
        prvno = each_line[211:217]
        psnam = each_line[217:232]
        ptype = each_line[232:242]
        pazip = each_line[242:251]
        pprov = each_line[251]
        lineCounter = each_line[252:260]
        ssvdt = each_line[260:268]
        ensvdt = each_line[268:276]
        enplsv = each_line[276:278]
        ensrsn = each_line[278:280]
        becat = each_line[280:283]
        diag1 = each_line[283:291]
        diag2 = each_line[291:299]
        diag3 = each_line[299:307]
        diag4 = each_line[307:315]
        diag5 = each_line[315:323]
        diag6 = each_line[323:331]
        diag7 = each_line[331:339]
        diag8 = each_line[339:347]
        diag9 = each_line[347:355]
        diag10 = each_line[355:363]
        endxf1 = each_line[363]
        endxf2 = each_line[364]
        endxf3 = each_line[365]
        endxf4 = each_line[366]
        pcdcd = each_line[367:376]
        emod1 = each_line[376:378]
        emod2 = each_line[378:380]
        emod3 = each_line[380:382]
        emod4 = each_line[382:384]
        pcdqt = each_line[384:387]
        pcdqt1 = each_line[387:389]
        bilam = each_line[389:400]
        netam = each_line[400:411]
        alwam = each_line[411:422]
        dctam = each_line[422:433]
        copam = each_line[433:444]
        ncvam = each_line[444:455]
        cobsv = each_line[455:466]
        ncrsn = each_line[466:468]
        revcd = each_line[468:472]
        drgcd = each_line[472:475]
        sprc1 = each_line[475:483]
        sprc2 = each_line[483:491]
        sprc3 = each_line[491:499]
        sprc4 = each_line[499:507]
        sprc5 = each_line[507:515]
        sprc6 = each_line[515:523]
        filetc = each_line[523:570]
        vvndno = each_line[570:580]
        vname = each_line[580:610]
        vadd1 = each_line[610:665]
        vadd2 = each_line[665:720]
        vcity = each_line[720:735]
        vstate = each_line[735:737]
        vzip = each_line[737:746]
        hpatc = each_line[746:784]
        icdver = each_line[784]

        newline = (filet + ',' + srcky + ',' + clmst + ',' + postd + ',' + rcvdt + ',' + mbrno + ',' + pelcd + ',' + plname + ',' + pmfnam + ',' + pmbidt + ',' + pmbsex + ',' + mmbrno + ',' + mlname + ',' + mmfnam + ',' + mmbsex + ',' + mmbidt + ',' + grpid + ',' + plncd + ',' + aprno + ',' + prvno + ',' + psnam + ',' + ptype + ',' + pazip + ',' + pprov + ',' + lineCounter + ',' + ssvdt + ',' + ensvdt + ',' + enplsv + ',' + ensrsn + ',' + becat + ',' + diag1 + ',' + diag2 + ',' + diag3 + ',' + diag4 + ',' + diag5 + ',' + diag6 + ',' + diag7 + ',' + diag8 +
                ',' + diag9 + ',' + diag10 + ',' + endxf1 + ',' + endxf2 + ',' + endxf3 + ',' + endxf4 + ',' + pcdcd + ',' + emod1 + ',' + emod2 + ',' + emod3 + ',' + emod4 + ',' + pcdqt + ',' + pcdqt1 + ',' + bilam + ',' + netam + ',' + alwam + ',' + dctam + ',' + copam + ',' + ncvam + ',' + cobsv + ',' + ncrsn + ',' + revcd + ',' + drgcd + ',' + sprc1 + ',' + sprc2 + ',' + sprc3 + ',' + sprc4 + ',' + sprc5 + ',' + sprc6 + ',' + filetc + ',' + vvndno + ',' + vname + ',' + vadd1 + ',' + vadd2 + ',' + vcity + ',' + vstate + ',' + vzip + ',' + hpatc + ',' + icdver)

        out_f.write(str(newline))

答案 1 :(得分:0)

除了@Joshua关于for循环的观察之外,您还可以将算法推广为更短的代码。 OP的代码中也可能有错误。 each_line[33]被跳过。如果有意,请使用以下代码:

import csv

# starts for each column
cols = (0,1,34,35,43,51,62,64,89,114,122,123,134,159,184,185,193,199,202,211,
        217,232,242,251,252,260,268,276,278,280,283,291,299,307,315,323,331,
        339,347,355,363,364,365,366,367,376,378,380,382,384,387,389,400,411,
        422,433,444,455,466,468,472,475,483,491,499,507,515,523,570,580,610,
        665,720,735,737,746,784,785)

# newline='' per csv documentation.
with open('file.txt') as f, open('file.csv','w',newline='') as out_f:
    writer = csv.writer(out_f)
    for each_line in f:
        line = []
        for i in range(len(cols)-1):
            # compute the slice for each column
            start,end = cols[i],cols[i+1]
            # This may be an error in OP's code, but each_line[33] is skipped.
            if end == 34:
                end = 33
            line.append(each_line[start:end])
        writer.writerow(line)