python - 如何使用python获取列表中单个字符串中的值而不用逗号分隔csv文件中的值?

标签 python python-3.x csv python-3.5

我保留了一个输入字段,可以在其中浏览和选择 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"']

最佳答案

如果您在 python 中使用 csv,那么您必须像导入 base64 一样导入 csv 库来使用它。

但如果您被限制不能使用 csv,还有其他解决方案。value = value.replace('"', '')value = value.replace( ''', '')

但这并不是更好的选择。更好的选择是使用 csv 模块

关于python - 如何使用python获取列表中单个字符串中的值而不用逗号分隔csv文件中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58427164/

相关文章:

c#-4.0 - 如何正确读取文本文件中的列

python - 在 to_csv 命令中选择 index=False 选项时,Excel 不打开 csv 文件

mysql - 如何将包含反斜杠的csv文件导入MySQL

python - 通过比较 Pandas 中前 n 行来获取列的最小值

Python 在 XML 元素中搜索数据

python - 限制 Spark 上下文中的记录数量

python - 列表索引超出范围错误 - 请求帮助

python - 使检查的字符串数量根据一行中的字符串数量而变化

python - 在字符串中的关键字之前删除 '\n'

python-3.x - Asyncio 标准输出 - 失败