0

I have kept an input field where i can browse and select the CSV file, and decode the file and passing the values to relevant fields. The problem I am receiving now is the values like

['hello','"hello', 'world"']

The cell values in the CSV file will be like

col1  col2    col3
hello world hello,world

The code I have tried:

import base64
file_value = self.file_import.decode("utf-8")
            filename, FileExtension = os.path.splitext(self.filename)
            input_file = base64.b64decode(file_value)
            lst = []
            for loop in input_file.decode("utf-8").split("\n"):
                l = loop.replace(u'\ufeff', '')
                vals = l.replace('\r', '')
                lst.append([vals])

            #  Deletes the heading of the csv file
            lst.pop(0)

            for res in lst:
                if res[0]:
                    output = res[0].split(',')
                    print(output)

The output is:

['hello','"hello', 'world"']

But I want like:

['hello','"hello,world"']
3
  • 1
    you should use module csv to write and read CSV file - it should handle it. Commented Oct 17, 2019 at 7:42
  • May I know how?@furas Commented Oct 17, 2019 at 8:33
  • import csv, reader = csv.reader(open(filename)) and data = list(reader) Commented Oct 17, 2019 at 8:37

1 Answer 1

1

If you are working with the csv's in python then you must use the csv library by importing it like you imported base64.

But if you are restricted to not to use csv, there is an other solution.value = value.replace('"', '') and value = value.replace(''', '')

But this is not a better option. Better option is to use csv module

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.