0

Hi I am new to python and I want to write a program which will read the csv file and write the data to another file.

Below is the code:

    #!/usr/bin/python

import csv

FEED_FILE = '/proj/ctc/temp/sanjay/REC-754/20170529_MUREX_EOD_REC_RATES_20170531.csv'
OUT_FILE = '/proj/ctc/temp/sanjay/REC-754/20170529_MUREX_EOD_REC_RATES_20170531_out.csv'

with open(OUT_FILE, 'w', newline='') as outputfile:
    with open(FEED_FILE, newline='') as feed:
    writer = csv.writer(outputfile, delimiter=',', quotechar='"')
        reader = csv.reader(feed, delimiter=',', quotechar='"')
        for row in reader: 
        row[3] = sorted(list(row[3].split(','))
                writer.writerow(row)

but I am getting below error, which I am not able to understand why?

$./Csvreader.py
  File "./Csvreader.py", line 14
    writer.writerow(row)
         ^
SyntaxError: invalid syntax

Please help me with the solution, thanks in advance.

2
  • 3
    Please check the indentation. Commented Jun 16, 2017 at 9:51
  • 2
    check the parentheses on the previous line. you forgot to close sorted(... Commented Jun 16, 2017 at 10:00

2 Answers 2

1

Try the following code:

with open(OUT_FILE, 'w', newline='') as outputfile:
    with open(FEED_FILE, newline='') as feed:
        writer = csv.writer(outputfile, delimiter=',', quotechar='"')
        reader = csv.reader(feed, delimiter=',', quotechar='"')
        for row in reader: 
            row[3] = sorted(list(row[3].split(',')))
            writer.writerow(row)
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is with the indentation!

with open(OUT_FILE, 'w', newline='') as outputfile:
    with open(FEED_FILE, newline='') as feed:
        writer = csv.writer(outputfile, delimiter=',', quotechar='"')
        reader = csv.reader(feed, delimiter=',', quotechar='"')
        for row in reader: 
            row[3] = sorted(list(row[3].split(','))
            writer.writerow(row)

EDIT: For your updated question, there is an indentation problem again!

this

           row[3] = sorted(list(row[3].split(','))
                writer.writerow(row)

should be

                row[3] = sorted(list(row[3].split(','))
                writer.writerow(row)

2 Comments

A different error would have been thrown in that case. If you look, the sorted call on the previous line is missing the closing parenthesis
Thanks for your response .. even this code is also giving the same error

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.