首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用导入csv读取csv

不使用导入csv读取csv
EN

Stack Overflow用户
提问于 2022-08-03 08:03:46
回答 1查看 28关注 0票数 -1

读取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'}

EN

回答 1

Stack Overflow用户

发布于 2022-08-03 08:26:30

唯一复杂的是,除了在引号中不使用CSV (我假设其他外部模块)之外,您还想在逗号上拆分

您可以使用来自Split by comma and how to exclude comma from quotes in split的纯函数标记

代码语言:javascript
复制
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")

输出

代码语言:javascript
复制
{'"name"': '"devendran"', '"sem"': '"1,2,3"', '"math"': '"90"', '"physics"': '"90"', '"chemistry"': '"90"'}
{'"name"': '"devendran"', '"sem"': '"4"', '"math"': '"99"', '"physics"': '"88"', '"chemistry"': '"77"'}

文件: test.txt

代码语言:javascript
复制
"name","sem","math","physics","chemistry" 
"devendran","1,2,3","90","90","90"
"devendran","4","99","88","77"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73218250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档