代码之家  ›  专栏  ›  技术社区  ›  stackoa

如何在Python中将csv的内容转换为小写?

  •  -1
  • stackoa  · 技术社区  · 7 年前

    我已经看了所有我能找到的线索,但我仍然没有弄清楚,我有各种各样的问题。

    我最近的尝试,但我已经尝试了很多事情。

    with open(teacherDD, 'r+') as f:
    read = csv.reader(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
    for row in read:
        copyRow = row.copy()
        # print(copyRow)
        del row[:]
        # print(row)
        getLowerStr = str(copyRow).lower()
        # appendLower = row.append(getLowerStr)
        # print(getLowerStr)
        print(row)
        f.write(getLowerStr)
    f.close()
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   cs95 abhishek58g    7 年前

    如果您只想转换成小写,为什么要使用csv阅读器?

    fileinput 用于在位编辑线的模块。

    Python3.x

    import fileinput
    
    for line in fileinput.input("test.txt", inplace=1):
        print(line.lower(), end='') 
    

    Python2.x

    import fileinput
    import sys
    
    for line in fileinput.input("test.txt", inplace=1):
        sys.stdout.write(line.lower())
    

    print sys.stdout.write 被重定向到文件。

    UPPER,CASE,ROW
    

    输出:

    upper,case,row