def test_text_to_csv(): """Test convert_eprime.convert.text_to_csv. """ text_file = join(get_test_data_path(), 'Cuetask-PILOT-1.txt') out_file = join(get_test_data_path(), 'out_csv.csv') convert.text_to_csv(text_file, out_file) df = pd.read_csv(out_file) assert df.shape == (81, 98) remove(out_file)
# emacs: -*- mode: python-mode; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*- # ex: set sts=4 ts=4 sw=4 et: """ Convert an example E-Prime text file to csv. """ from convert_eprime import convert from os.path import join DATA_DIR = '/Users/salo/Downloads' TEXT_FILE = join(DATA_DIR, '150609_CART_send5forscanner_withtracking-43-1.txt') EDAT_FILE = join(DATA_DIR, '150609_CART_send5forscanner_withtracking-43-1.edat2') OUT_FILE = join(DATA_DIR, 'pilot_subj.csv') convert.text_to_csv(TEXT_FILE, OUT_FILE)
# In[2]: from convert_eprime.convert import text_to_csv file_directory = join(data_dir, "eprime_files") for filename in os.listdir(file_directory): if filename.endswith("txt"): text_file = join(file_directory, filename) out_file = join(join(data_dir, "csv_files"), filename[:-4] + ".csv") with open(text_file, encoding="latin1") as fo: raw_data = fo.readlines()[:20] raw_data = [l.rstrip() for l in raw_data] # Remove unicode characters. filtered_data = [remove_unicode(row) for row in raw_data] print('The raw text file (after removing unicode characters):') for l in filtered_data: print(l) print('') text_to_csv(text_file, out_file) print('') df = pd.read_csv(out_file) print('The converted csv file:') print(df.head(10))
from os import remove from os.path import join import glob import pandas as pd from convert_eprime.utils import remove_unicode from convert_eprime.convert import text_to_csv path = '/Users/ben/Documents/FIBRO_BEHAV_DATA_HC/' csv_names = glob.glob(path + '*.txt') for i in csv_names: filename = i[:-4] outfile = ''.join(filename + '.csv') with open(i, 'r') as fo: raw_data = fo.readlines()[:20] raw_data = [l.rstrip() for l in raw_data] # Remove unicode characters. filtered_data = [remove_unicode(row) for row in raw_data] text_to_csv(i, outfile)
""" Convert all txt files in current directory to csvs with the same file name. """ import sys sys.path.append('../') from convert_eprime import convert import os for file in os.listdir("."): if file.endswith(".txt"): out_file = file[:-3] + "csv" convert.text_to_csv(file, out_file)