我的代码运行并正确选择文件夹中的文件。问题是它似乎没有在一行(从0到81923都是整数)上写标题,以及文本文件本身。这是代码:
import os
import csv
import itertools
#path where the Neuroventure Participant data is stored
rpath = 'C:\Neuroventure\PreprocessedData'
#participant ID number
part_nb = 1
#adding header as integer values
with open ('C:\Users\SPISEA00\Documents\CSV files\NV_CT.csv', 'wb') as outcsv:
wr = csv.writer(outcsv)
wr.writerow([x for x in range(81924)])
#find files and write to csv
for dirpath, dirnames, files in os.walk(rpath):
for f in files:
if "V1" in f and str(part_nb) in f and "native_rms_rsl_tlink" in f and "30mm" in f and "asym" not in f and "left" not in f and "right" not in f:
fullpath = os.path.join(dirpath, f)
print f
with open(fullpath, 'r') as in_file:
stripped = (line.strip() for line in in_file)
lines = (line for line in stripped if line)
grouped = itertools.izip(*[lines] * 81924)
with open('C:\Users\SPISEA00\Documents\CSVfiles\NV_CT.csv', 'a') as out_file:
writer = csv.writer(out_file)
writer.writerows(grouped)
part_nb = part_nb + 1
if part_nb == 121:
part_nb = part_nb + 1
if part_nb == 124:
part_nb = part_nb + 1
if part_nb == 127:
part_nb = part_nb + 1
if part_nb == 128:
part_nb = part_nb + 1
我认为由于文本文件格式可能会出现一些复杂情况,但我无法确定。知道为什么它不会简单地写在一行上吗?以下是其中一个文本文件的示例:
3.758533.070973.394973.124063.320233.301132.749053.422063.334462.626923.048533.717