csv 文件中的语法无效 ipython

发布于 2025-01-14 22:22:37 字数 1180 浏览 3 评论 0原文

这些是我作为 csv 文件的输入,但由于无效语法错误,我无法在 ipython 中运行我的代码,但我不知道该怎么办?

mandana,5,7,3,15
hamid,3,9,4,20,9,1,8,16,0,5,2,4,7,2,1
sina,0,5,20,14
soheila,13,2,5,1,3,10,12,4,13,17,7,7
ali,1,9
sarvin,0,16,16,13,19,2,17,8
def calculate_sorted_averages('C:\Users\Y  A  S  H  E  L\Desktop\in.csv','C:\Users\Y  A  S  H  E  L\Desktop\o.csv'):
    averages = {}
    with open('C:\Users\Y  A  S  H  E  L\Desktop\in.csv') as csv_file:
        csvfile = csv.reader(csv_file, delimiter=',')
        for row in csvfile:
            scores = []
            for i in range(1, len(row)):
                scores.append (float(row[i]))
            avg = mean(scores)
            averages [row[0]] = avg

    averages_ord = OrderedDict (sorted (averages.items(), key=lambda x:(x[1], x[0])))

    with open ('C:\Users\Y  A  S  H  E  L\Desktop\o.csv', 'w') as out:
        count = 0
        for person in averages_ord:
            count += 1
            if count == 1:
                out.write(person+ ","+ str(averages_ord[person]))
            else:
                out.write("\n"+ person+ ","+ str(averages_ord[person]))

these are my input as a csv file but I can not run my code in ipython because of invalid syntax error but I do not know what should I do?

mandana,5,7,3,15
hamid,3,9,4,20,9,1,8,16,0,5,2,4,7,2,1
sina,0,5,20,14
soheila,13,2,5,1,3,10,12,4,13,17,7,7
ali,1,9
sarvin,0,16,16,13,19,2,17,8
def calculate_sorted_averages('C:\Users\Y  A  S  H  E  L\Desktop\in.csv','C:\Users\Y  A  S  H  E  L\Desktop\o.csv'):
    averages = {}
    with open('C:\Users\Y  A  S  H  E  L\Desktop\in.csv') as csv_file:
        csvfile = csv.reader(csv_file, delimiter=',')
        for row in csvfile:
            scores = []
            for i in range(1, len(row)):
                scores.append (float(row[i]))
            avg = mean(scores)
            averages [row[0]] = avg

    averages_ord = OrderedDict (sorted (averages.items(), key=lambda x:(x[1], x[0])))

    with open ('C:\Users\Y  A  S  H  E  L\Desktop\o.csv', 'w') as out:
        count = 0
        for person in averages_ord:
            count += 1
            if count == 1:
                out.write(person+ ","+ str(averages_ord[person]))
            else:
                out.write("\n"+ person+ ","+ str(averages_ord[person]))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2025-01-21 22:22:37

当我将你的函数复制到 python 会话时,我得到:

    def calculate_sorted_averages('C:\Users\Y  A  S  H  E  L\Desktop\in.csv','C:\Users\Y  A  S  H  E  L\Desktop\o.csv'):
                                  ^
SyntaxError: invalid syntax

You can Define a function with

def foo(filename1, filename2):

You can't Define it with aliteral string, def foo('test.txt'):

语法错误意味着你的代码在基本的 Python 语法级别上是错误的。它甚至不会尝试运行您的代码。

这纠正了该语法错误。我还没有尝试运行它。

def calculate_sorted_averages(file1, file2):
    averages = {}
    with open(file1) as csv_file:
        csvfile = csv.reader(csv_file, delimiter=",")
        for row in csvfile:
            scores = []
            for i in range(1, len(row)):
                scores.append(float(row[i]))
            avg = mean(scores)
            averages[row[0]] = avg

    averages_ord = OrderedDict(sorted(averages.items(), key=lambda x: (x[1], x[0])))

    with open(file2, "w") as out:
        count = 0
        for person in averages_ord:
            count += 1
            if count == 1:
                out.write(person + "," + str(averages_ord[person]))
            else:
                out.write("\n" + person + "," + str(averages_ord[person]))

When I copy your function to a python session I get:

    def calculate_sorted_averages('C:\Users\Y  A  S  H  E  L\Desktop\in.csv','C:\Users\Y  A  S  H  E  L\Desktop\o.csv'):
                                  ^
SyntaxError: invalid syntax

You can define a function with

def foo(filename1, filename2):

You can not define it with a literal string, def foo('test.txt'):

A syntax error means your code is wrong at a basic Python syntax level. It doesn't even try to run your code.

This corrects that syntax error. I haven't tried to run it.

def calculate_sorted_averages(file1, file2):
    averages = {}
    with open(file1) as csv_file:
        csvfile = csv.reader(csv_file, delimiter=",")
        for row in csvfile:
            scores = []
            for i in range(1, len(row)):
                scores.append(float(row[i]))
            avg = mean(scores)
            averages[row[0]] = avg

    averages_ord = OrderedDict(sorted(averages.items(), key=lambda x: (x[1], x[0])))

    with open(file2, "w") as out:
        count = 0
        for person in averages_ord:
            count += 1
            if count == 1:
                out.write(person + "," + str(averages_ord[person]))
            else:
                out.write("\n" + person + "," + str(averages_ord[person]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文