简体   繁体   中英

How to get values with in a single string in a list without getting separated by commas in csv file using python?

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"']

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM