读取csv而不使用python中的导入csv
“名称”、"sem“、”数学“、”物理“、”化学“"devendran”、“1,2、3”、"90“、"90”、"90“、"devendran”、"4“、"99”、"88“、"77”
预期产出为
{'name':'devendran','sem':'1,2,3',‘数学’:'90',‘物理’,'90',‘化学’} {'name':'devendran','sem':'4',‘数学’:'99',‘物理’:'88',‘化学’:'77'}
发布于 2022-08-03 08:26:30
唯一复杂的是,除了在引号中不使用CSV (我假设其他外部模块)之外,您还想在逗号上拆分
您可以使用来自Split by comma and how to exclude comma from quotes in split的纯函数标记
码
def tokenize( string, separator = ',', quote = '"' ):
"""
Split a comma separated string into a List of strings.
Separator characters inside the quotes are ignored.
:param string: A string to be split into chunks
:param separator: A separator character
:param quote: A character to define beginning and end of the quoted string
:return: A list of strings, one element for every chunk
"""
comma_separated_list = []
chunk = ''
in_quotes = False
for character in string:
if character == separator and not in_quotes:
comma_separated_list.append(chunk)
chunk = ''
else:
chunk += character
if character == quote:
in_quotes = False if in_quotes else True
comma_separated_list.append( chunk )
return comma_separated_list
# Read file 'test.txt'
with open('test.txt', 'r') as f:
result = []
for i, line in enumerate(f):
line = line.rstrip() # remove trailing '\n'
line = tokenize(line) # split on "," except for quotes
if i == 0:
header = line # header row
else:
# Dictionary comprehension for header, value pairs
result.append({h:l for h, l in zip(header, line)})
print(*result, sep = "\n")输出
{'"name"': '"devendran"', '"sem"': '"1,2,3"', '"math"': '"90"', '"physics"': '"90"', '"chemistry"': '"90"'}
{'"name"': '"devendran"', '"sem"': '"4"', '"math"': '"99"', '"physics"': '"88"', '"chemistry"': '"77"'}文件: test.txt
"name","sem","math","physics","chemistry"
"devendran","1,2,3","90","90","90"
"devendran","4","99","88","77"https://stackoverflow.com/questions/73218250
复制相似问题