0
votes

I have a folder full of .mpt files, each of them having the same data format. I need to delete the first 57 lines from all files and append these files into one csv - output.csv. I have that section already:

import glob
import os

dir_name = 'path name'
lines_to_ignore = 57
input_file_format = '*.mpt'
output_file_name = "output.csv"

def convert():
    files = glob.glob(os.path.join(dir_name, input_file_format))
    with open(os.path.join(dir_name, output_file_name), 'w') as out_file:
        for f in files:
            with open(f, 'r') as in_file:
                content = in_file.readlines()
                content = content[lines_to_ignore:]
                for i in content:
                    out_file.write(i)

print("working")
convert()
print("done")

This part works ok.

how do i add the filename of each .mpt file as the last column of the output.csv Thank you!

2
Change the last line of convert() to out_file.write(",".join([i,f]))? - kdopen
use csv.reader and append the filename to the last row - Padraic Cunningham

2 Answers

1
votes

This is a quick 'n dirty solution.

In this loop the variable i is just a string (a line from a CSV file):

            for i in content:
                out_file.write(i)

So you just need to 1) strip off the end of line character(s) (either "\n" or "\r\n") and append ",".

If you're using Unix, try:

for i in content:
  i = i.rstrip("\n") + "," + output_file_name + "\n"
  out_file.write(i)

This assumes that the field separator is a comma. Another option is:

for i in content:
  i = i.rstrip() + "," + output_file_name
  print >>out_file, i

This will strip all white space from the end of i.

Add quotes if you need to quote the output file name:

  i = i.rstrip(...) + ',"' + output_file_name '"'
0
votes

The relevant part:

with open(f, 'r') as in_file:
    content = in_file.readlines()
    content = content[lines_to_ignore:]
    for i in content:   
        new_line = ",".join([i.rstrip(), f]) + "\n" #<-- this is new
        out_file.write(new_line)                    #<-- this is new