gpt4 book ai didi

python - 使用 python 将 txt 文件的每一行和部分部分读取到 csv 文件中

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:04 24 4
gpt4 key购买 nike

我有一个 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

最佳答案

除了 @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)

关于python - 使用 python 将 txt 文件的每一行和部分部分读取到 csv 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53981944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com