如何使用python在csv文件中的逗号中获取列表中单个字符串的值?

问题描述 投票:1回答:1

我保留了一个输入字段,可以在其中浏览和选择CSV文件,并对文件进行解码并将值传递给相关字段。我现在收到的问题是像>>这样的值

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

CSV文件中的单元格值将类似于

col1  col2    col3
hello world hello,world

我尝试过的代码:

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)

输出为:

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

但是我想要:

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

我保留了一个输入字段,可以在其中浏览和选择CSV文件,并对文件进行解码并将值传递给相关字段。我现在收到的问题是[[hello','“ ...

python python-3.x csv python-3.5
1个回答
0
投票

如果您使用python中的csv,则必须像导入base64一样通过导入csv库来使用它。

© www.soinside.com 2019 - 2024. All rights reserved.