Eprime outputs a .txt file like this:
*** Header Start *** VersionPersist: 1 LevelName: Session Subject: 7 Session: 1 RandomSeed: -1983293234 Group: 1 Display.RefreshRate: 59.654 *** Header End *** Level: 2 *** LogFrame Start *** MeansEffectBias: 7 Procedure: trialProc itemID: 7 bias1Answer: 1 *** LogFrame End *** Level: 2 *** LogFrame Start *** MeansEffectBias: 2 Procedure: trialProc itemID: 2 bias1Answer: 0
I want to parse this and write it to a .csv file but with a number of lines deleted.
I tried to create a dictionary that took the text appearing before the colon as the key and the text after as the value:
{subject: [7, 7], bias1Answer : [1, 0], itemID: [7, 2]}
def load_data(filename): data = {} eprime = open(filename, 'r') for line in eprime: rows = re.sub('\s+', ' ', line).strip().split(':') try: data[rows[0]] += rows[1] except KeyError: data[rows[0]] = rows[1] eprime.close() return data
for line in open(fileName, 'r'): if ':' in line: row = line.strip().split(':') fullDict[row[0]] = row[1] print fullDict
both of the scripts below produce garbage:
{'\x00\t\x00M\x00e\x00a\x00n\x00s\x00E\x00f\x00f\x00e\x00c\x00t\x00B\x00i\x00a\x00s\x00': '\x00 \x005\x00\r\x00', '\x00\t\x00B\x00i\x00a\x00s\x002\x00Q\x00.\x00D\x00u\x00r\x00a\x00t\x00i\x00o\x00n\x00E\x00r\x00r\x00o\x00r\x00': '\x00 \x00-\x009\x009\x009\x009\x009\x009\x00\r\x00'
If I could set up the dictionary, I can write it to a csv file that would look like this!!:
Subject itemID ... bias1Answer 7 7 1 7 2 0
Converting text files to CSV For PC, head to the "File" menu, and choose "Save as". You'll type in the name of your file, and add the extension ". csv". This will automatically change it to CSV format!
To write a dictionary of list to CSV files, the necessary functions are csv. writer(), csv. writerow(). This method writes a single row at a time.
DictWriter. writeheader() is used to write a row of column headings / field names to the given CSV file.
You don't need to create dictionary.
import codecs
import csv
with codecs.open('eprime.txt', encoding='utf-16') as f, open('output.csv', 'w') as fout:
writer = csv.writer(fout, delimiter='\t')
writer.writerow(['Subject', 'itemID', 'bias1Answer'])
for line in f:
if ':' in line:
value = line.split()[-1]
if 'Subject:' in line:
subject = value
elif 'itemID:' in line:
itemID = value
elif 'bias1Answer:' in line:
bias1Answer = value
writer.writerow([subject, itemID, bias1Answer])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With