Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Vectorizing using numpy
#1
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") 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Vectorizing a quadruple for loop in python winash12 13 1,632 Aug-29-2023, 07:42 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020