をありがとう - (いくつかの明白な場所がコメントしている)いくつかの変更が必要とされているから2.xのPython用
のデータ形式について質問、これはPythonで出発点のようになります。
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ビッグですか?それは記憶に保持できますか? –