regex - Remove first and last character from csv file using python -
i need transpose following file output1.csv, is result quantum chemistry calculation 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 wrote code:
import os import csv import re import sys import pandas pd inputfile = open('output1.csv', 'r') reader = csv.reader(inputfile) outputfile = open('output1_f.csv', 'a') writer = csv.writer(outputfile) open('output1_f.csv', 'w') file: file.write('frequencies,frequencies,frequencies\n') row in reader: row = [re.sub(' +', ',', item) item in row] row = [re.sub(',frequencies,--,', '', item) item in row] # row = map(str.strip, row) writer.writerow(row) inputfile.close() outputfile.close()
i added code comment in order delete first , last character " every line in output1_f.csv file. not work out.
row = map(str.strip, row)
i found line.replace solution creates second output1_2f.csv file.
inputfile = open('output1_f.csv', 'r') outputfile = open('output1_2f.csv', 'w') line in inputfile: line = line.replace('"', '') outputfile.write(line) inputfile.close() outputfile.close()
the following transpose part works if character " removed , reason need delete " character efficiently code instead of line.replace.
ifile = open('output1_2f.csv', "rb") reader = csv.reader(ifile) open('output1_transp.csv', 'w') out: rownum = 0 row in reader: # save header row. if rownum == 0: header = row else: colnum = 0 col in row: out.write( '%s\n' % (col)) colnum += 1 rownum += 1 ifile.close()
i grateful if propose me idea shorten code , make more efficient , easier use. thank developers time!!!
the user https://codereview.stackexchange.com/users/39848/edward helped here
with open('input.txt', 'r') infile, open('out.csv', 'w') outfile: print >> outfile, "frequency" line in infile: freq in line.split()[2:]: print >> outfile, freq
Comments
Post a Comment