Python Forum

Full Version: Vectorizing using numpy
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm relatively new to Python and have the following Python code. This code actually reads every line from a .csv file and does some actions.

I have used numpy successfully in the past and it has helped me in making my numerical computations very fast. However, this code is to do with reading from a csv file and thus can anyone advise if numpy usage will be better off here?

Regards
John

import sys
import os

#sys.argv = [" " , "Ex_1.csv"]

site_seperators = [';']
comment_characters = ['#']

cleaned_file = []
with open(sys.argv[1].strip(), 'r') as control_file:
    for line in control_file:
        line = ' '.join(str(x) for x in line.strip().strip(',').split())

        if any ( char in line for char in comment_characters):        
           for i in range(0, len(line), 1):
               if line[i] in comment_characters:
                   end = i
                   break
           line = line[0:i]
           line = line.strip().strip(',')

        if any (char in line for char in site_seperators):
            for x in site_seperators:
                line = line.replace(x,',')

        for item in line.split(','):
            if os.sep in item or os.altsep in item:
                if os.path.isfile(item) == False:
                    if os.path.exists(item) == False:
                        sys.exit("--XX Execution Stopped! File not found --> " +str(item) +" XX--")
        if len(line) != 0:
            cleaned_file.append(line)

with open('control_in.csv', 'w') as fout:
    for i in cleaned_file:
        fout.write(i+"\n")