이 답변 파이썬 3.x를를 사용하는 가정 - 파이썬은 약간의 변화는 데이터 형식에 대한
에서 (일부 명백한 장소 주석) 필요 2.x에서 문제는,이 파이썬에서 시작하는 포인트가 될 수있다 : 실제로
import collections
def slide(infile,outfile,window_size):
queue=collections.deque(maxlen=window_size)
line=infile.readline()
headers=[s.strip() for s in line.split("|")]
row=[headers[0]]
for h in headers[1:]
for i in reversed(range(window_size)):
row.append("%s-%i"%(h,i))
outfile.write(" | ".join(row))
outfile.write("\n")
for line in infile:
queue.append(line.split())
if len(queue)==window_size:
row=[queue[-1][0]]
for j in range(1,len(headers)):
for old in queue:
row.append(old[j])
outfile.write("\t".join(row))
outfile.write("\n")
ws=3
with open("infile.csv","r") as inf:
with open("outfile.csv","w") as outf:
slide(inf,outf,ws)
이 코드는 모든 윈도우의 입력 행을 유지하기 위해 큐를 사용하는 방법에 대한 그 밖의 모든 것은 텍스트 - 목록 - 투 - 텍스트입니다. 실제 CSV 데이터로
,
import csv
import collections
def slide(infile,outfile,window_size):
r=csv.reader(infile)
w=csv.writer(outfile)
queue=collections.deque(maxlen=window_size)
headers=next(r) # r.next() on python 2
l=[headers[0]]
for h in headers[1:]
for i in reversed(range(window_size)):
l.append("%s-%i"%(h,i))
w.writerow(l)
hrange=range(1,len(headers))
for row in r:
queue.append(row)
if len(queue)==window_size:
l=[queue[-1][0]]
for j in hrange:
for old in queue:
l.append(old[j])
w.writerow(l)
ws=3
with open("infile.csv","r") as inf: # rb and no newline param on python 2
with open("outfile.csv","w") as outf: # wb and no newline param on python 2
slide(inf,outf,ws)
당신이 [CSV (https://docs.python.org/3/library/csv.html) 모듈을 살펴 있었나요 (주석 참조) 아직? – janbrohl
이것은 실제 csv처럼 보이지 않으므로 문자열 메소드를 사용하는 것이 더 쉬울 수 있습니다. – janbrohl
은 (는) 당신의 CSV가 큽니까? 기억에 남을 수 있습니까? –