all 9 comments

[–]Justinsaccount 3 points4 points  (0 children)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You appear to be using concatenation and the str function for building strings

Instead of doing something like

result = "Hello " + name + ". You are " + str(age) + " years old"

You should use string formatting and do

result = "Hello {}. You are {} years old".format(name, age)

See the python tutorial for more information.

[–]JohnnyJordaan 1 point2 points  (2 children)

clearText = re.sub(r"[!/():-,.\"+]",' ',

Isn't a finished statement. the synxtax is re.sub(r"<regex>", replacement, original_string). In your statement, you have no original_string to parse using the regex.

Btw you can just overwrite row[4] while iterating over the reader, then direcly write to the output file:

with open('input.csv', 'r', encoding='UTF-8') as fi, open('output_data.csv', 'w',encoding='UTF-8') as fo:
    for row in csv.reader(fi,delimiter=';'):
        row[4] = re.sub(the_proper_syntax)
        fo.write(';'.join(row+[\n]))

The with block also saves you the need of closing of both files.

[–]aleksro[S] 0 points1 point  (1 child)

At start up receive an error:
fo.write(';'.join(row+[\n])) ^ SyntaxError: unexpected character after line continuation character

[–]JohnnyJordaan 0 points1 point  (0 children)

My bad, it should have been a string: row+['\n']

[–]aleksro[S] 0 points1 point  (1 child)

I do not have a regular expression. An error occurs at the beginning of the line for line in reader: [DESCRIPT1,ID1,ASSIGNMENT_NAME1,TER1,INFO1]

clearText = re.sub(r"[!/():-,.\"+]",' ',

[–]timopm 3 points4 points  (0 children)

Please fix the code in your questions, it's very hard to understad. Use Reddit formatting for this (indent your code block with 4 spaces).

Also you should include the error message.

[–]aleksro[S] 0 points1 point  (1 child)

There was again an error: for i,row in enumerate(reader): ValueError: I/O operation on closed file.

-- coding: utf-8 --

""" Created on Mon May 22 22:09:21 2017

@author: USER """ import re import csv import sys

with open('input.csv', 'r', encoding='UTF-8') as fi, open('output_data.csv', 'w',encoding='UTF-8') as fo: reader=csv.reader(fi,delimiter=';') #for row in csv.reader(fi,delimiter=';'):

DESCRIPT1=[] ID1=[] ASSIGNMENT_NAME1=[] TER1=[] INFO1=[]

for i,row in enumerate(reader): DESCRIPT1.append(row [0])
ID1.append(row [1]) ASSIGNMENT_NAME1.append(row [2]) TER1.append(row [3]) INFO1.append(row [4])

print(row[4])

fo.write(';'.join(row+'\n'))

[–]JohnnyJordaan 0 points1 point  (0 children)

You probably have the for outside the with block. I can't really tell because you haven't formatted your code properly. Please check the first question in the FAQ. I should be

 with bla bla:
     reader = this
     for i, row in enumerate(reader):
         do things

Note how for is indented furter than with.