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