I need to transpose the following file output1.csv, which is is a result from a quantum chemistry calculation into a single colum efficiently:
Frequencies -- 18.8210 44.7624 46.9673
Frequencies -- 66.6706 102.0432 112.4930
Frequencies -- 124.4601 138.4393 180.1404
Frequencies -- 230.0306 240.4389 258.2459
Frequencies -- 282.7781 340.8302 357.7789
Frequencies -- 378.9043 384.1284 401.4285
Frequencies -- 418.0523 444.2264 447.6885
Frequencies -- 473.2391 501.0937 518.9083
Frequencies -- 559.5925 609.9256 623.7729
Frequencies -- 657.4144 672.5480 728.2009
Frequencies -- 740.5035 750.3238 757.2199
Frequencies -- 774.6343 806.7750 815.9990
Frequencies -- 839.3050 858.0716 876.1641
Frequencies -- 888.6654 942.2963 965.7888
Frequencies -- 987.3819 994.7388 1020.8724
Frequencies -- 1025.0426 1045.5129 1059.0966
Frequencies -- 1076.5127 1143.1178 1155.4200
Frequencies -- 1208.6790 1219.7513 1244.7080
Frequencies -- 1265.6108 1287.8830 1300.0463
Frequencies -- 1325.0427 1339.0678 1353.0061
Frequencies -- 1369.0614 1408.5258 1433.0543
Frequencies -- 1452.4148 1454.6319 1500.4304
Frequencies -- 1511.2305 1517.2562 1552.9189
Frequencies -- 1560.5313 1636.2290 1640.1732
Frequencies -- 1664.8747 1681.5566 1703.2026
Frequencies -- 1770.2627 3058.4143 3122.3743
Frequencies -- 3147.1828 3192.5897 3199.1398
Frequencies -- 3211.0676 3222.0033 3236.3394
Frequencies -- 3262.2119 3556.7997 3862.4791
To achieve that I wrote this code:
import os
import csv
import re
import sys
import pandas as pd
inputfile = open('output1.csv', 'r')
reader = csv.reader(inputfile)
outputfile = open('output1_f.csv', 'a')
writer = csv.writer(outputfile)
with open('output1_f.csv', 'w') as file:
file.write('Frequencies,Frequencies,Frequencies\n')
for row in reader:
row = [re.sub(' +', ',', item) for item in row]
row = [re.sub(',Frequencies,--,', '', item) for item in row]
# row = map(str.strip, row)
writer.writerow(row)
inputfile.close()
outputfile.close()
I added the code as comment in order to delete the first and last character " from every line in the output1_f.csv file. However it does not work out.
row = map(str.strip, row)
I found the line.replace solution which creates a second output1_2f.csv file.
inputfile = open('output1_f.csv', 'r')
outputfile = open('output1_2f.csv', 'w')
for line in inputfile:
line = line.replace('"', '')
outputfile.write(line)
inputfile.close()
outputfile.close()
The following transpose part works only if the character " is removed and this is the reason that I need to delete the " character efficiently code instead of line.replace.
ifile = open('output1_2f.csv', "rb")
reader = csv.reader(ifile)
with open('output1_transp.csv', 'w') as out:
rownum = 0
for row in reader:
# Save header row.
if rownum == 0:
header = row
else:
colnum = 0
for col in row:
out.write( '%s\n' % (col))
colnum += 1
rownum += 1
ifile.close()
I would be grateful if you could propose me any idea to shorten the code and make it more efficient and easier to use. Thank you developers for your time!!!