读取 JSON 文件,将嵌套列表写入 CSV 文件。

huangapple go评论62阅读模式
英文:

Read JSON file, write nested lists to CSV file

问题

我已经完成了。以下是您的代码的翻译部分:

class SatData:
    """从JSON文件读取数据然后将数据以CSV格式写入.txt文件的类"""

    def __init__(self):
        try:
            with open('sat.json', 'r') as json_file:
                self._data_list = json.load(json_file)
        except FileNotFoundError:
            print('文件未找到')

    def save_as_csv(self, dbn_list):
        csv_list = []
        rows = ['DBN', '学校名称', '考试参与人数',
                '关键阅读平均分', '数学平均分', '写作平均分']
        for item in self._data_list['data']:
            if item[8] in dbn_list:
                csv_list.append(','.join(map(str, item[8:14])))
        csv_list.sort()
        with open('output.csv', 'w') as new_file:
            new_file.write(','.join(rows))
            new_file.write('\n')
            pos = 0
            while pos < len(csv_list):
                new_file.write(csv_list[pos])
                new_file.write('\n')
                pos += 1
            return csv_list


sd = SatData()
dbns = ["02M303", "02M294", "01M450", "02M418"]
sd.save_as_csv(dbns)

请注意,我进行了一些修改,以便更好地格式化CSV数据并对其进行排序。希望这可以解决您的问题。

英文:

WE ARE NOT ALLOWED TO IMPORT ANY MODULES INTO PYTHON EXCEPT THOSE ALLOWED IN THE PROMPT

I have an assignment I am working on in class. The prompt is to write a class that reads data from a json file and then writes the data to a .txt file in CSV format.

We then need to create a method named save_as_csv that takes as a parameter a list of DBNs (district bureau numbers) and saves a CSV file (example of what it needs to look like below) but with only the rows that correspond to the DBNs in the list (and also the row of column headers). You may assume that all of the DBNs in the list passed to your method are present in the JSON file. The rows in the CSV file must be sorted in ascending order by DBN. The name of the output file must be output.csv.

I have all of that done. But my output.csv file is not formatting correctly.

Here is my code so far:

import json
&quot;&quot;&quot;import json module&quot;&quot;&quot;


class SatData:
    &quot;&quot;&quot;reads a json files and writes it to a CSV file&quot;&quot;&quot;

def __init__(self):
    try:
        with open(&#39;sat.json&#39;, &#39;r&#39;) as json_file:
            self._data_list = json.load(json_file)
    except FileNotFoundError:
        print(&#39;file not found&#39;)

def save_as_csv(self, dbn_list):
    csv_list = []
    rows = [&#39;DBN&#39;, &#39;School Name&#39;, &#39;Number of Test Takers&#39;,
            &#39;Critical Reading Mean&#39;, &#39;Mathematics Mean&#39;, &#39;Writing Mean&#39;]
    for item in self._data_list[&#39;data&#39;]:
        if item[8] in dbn_list:
            csv_list.append(str(item[8:14]))
    sorted(csv_list)
    with open(&#39;output.csv&#39;, &#39;w&#39;) as new_file:
        new_file.write(&#39;,&#39;.join(rows))
        new_file.write(&#39;\n&#39;)
        pos = 0
        while pos &lt; len(csv_list):
            new_file.write(csv_list[pos])
            new_file.write(&#39;\n&#39;)
            pos += 1
        return csv_list


sd = SatData()
dbns = [&quot;02M303&quot;, &quot;02M294&quot;, &quot;01M450&quot;, &quot;02M418&quot;]
sd.save_as_csv(dbns)

My expected out put is:

DBN,School Name,Number of Test Takers,Critical Reading Mean,Mathematics Mean,Writing Mean
01M450,East Side Community High School,69,418,431,402
02M294,HIGH SCHOOL FOR HIST AND COMM,51,382,364,366
02M303,The Facing History School,59,381,373,377
02M418,Millennium High School,140,512,554,523

The output I am getting:

DBN,School Name,Number of Test Takers,Critical Reading Mean,Mathematics Mean,Writing Mean
[&#39;01M450&#39;, &#39;East Side Community High School &#39;, &#39;69&#39;, &#39;418&#39;, &#39;431&#39;, &#39;402&#39;]
[&#39;02M294&#39;, &#39;HIGH SCHOOL FOR HIST AND COMM &#39;, &#39;51&#39;, &#39;382&#39;, &#39;364&#39;, &#39;366&#39;]
[&#39;02M303&#39;, &#39;The Facing History School &#39;, &#39;59&#39;, &#39;381&#39;, &#39;373&#39;, &#39;377&#39;]
[&#39;02M418&#39;, &#39;Millennium High School &#39;, &#39;140&#39;, &#39;512&#39;, &#39;554&#39;, &#39;523&#39;]

Long story short, I know that the elements in csv_list is just a nested list, but how can I get it to display without the brackets and without the single quotes around the data?

I have tried the following:

*csv_list, sep = &#39;,&#39;

that just gives me output with each individual character separated by a comma

&#39;,&#39;.join(csv_list)

ANY IDEAS? The thing is if I try to split the csv_list, then it will make an unknown number of new lists, depending on how many matching DBN's there are, so that won't work. I don't know what else to try.

thanks!

答案1

得分: 0

我相信你的问题出在这一行上:csv_list.append(str(item[8:14])),当你将一个列表(item[8:14])转换为字符串时,会得到带引号的括号。更好的做法是使用csv_list.append(','.join(item[8:14])),或者你可以使用列表推导式来去掉空格:csv_list.append(','.join(a.strip() for a in item[8:14]))

英文:

I believe your issue falls on this line: csv_list.append(str(item[8:14]))
when you convert a list (item[8:14]) to a string, you get the brackets with the quotes. A better way to do this would be csv_list.append(&#39;,&#39;.join(item[8:14])), or you could use list comprehension to remove the spacing: csv_list.append(&#39;,&#39;.join(a.strip() for a in item[8:14]))

答案2

得分: 0

看起来csv_list是一个列表的列表?在这种情况下,可以使用带有join列表推导。在你的情况下,学校名称中似乎包含一些空格,你可以对要连接的列表中的每个元素使用strip。你还可以使用print来将数据写入输出文件,以处理换行符。

rows = ['DBN', 'School Name', 'Number of Test Takers',
        'Critical Reading Mean', 'Mathematics Mean', 'Writing Mean']

csv_list = [
    ['01M450', 'East Side Community High School ', '69', '418', '431', '402'],
    ['02M294', 'HIGH SCHOOL FOR HIST AND COMM ', '51', '382', '364', '366'],
    ['02M303', 'The Facing History School ', '59', '381', '373', '377'],
    ['02M418', 'Millennium High School ', '140', '512', '554', '523'],
]

csv_list = [','.join(j.strip() for j in i) for i in csv_list]

with open('output.csv', 'w') as new_file:
    print(','.join(rows), file=new_file)
    for row in csv_list: print(row, file=new_file)

https://onlinegdb.com/yThIfUFWy

英文:

it looks like csv_list is a list of lists? in that case, list comprehension with join will work. in your case it looks like there is some whitespace in the school name, you can use strip for each element in the list you are joining. you can also print to the outfile to take care of the newline for you.

rows = [&#39;DBN&#39;, &#39;School Name&#39;, &#39;Number of Test Takers&#39;,
        &#39;Critical Reading Mean&#39;, &#39;Mathematics Mean&#39;, &#39;Writing Mean&#39;]

csv_list = [
    [&#39;01M450&#39;, &#39;East Side Community High School &#39;, &#39;69&#39;, &#39;418&#39;, &#39;431&#39;, &#39;402&#39;],
    [&#39;02M294&#39;, &#39;HIGH SCHOOL FOR HIST AND COMM &#39;, &#39;51&#39;, &#39;382&#39;, &#39;364&#39;, &#39;366&#39;],
    [&#39;02M303&#39;, &#39;The Facing History School &#39;, &#39;59&#39;, &#39;381&#39;, &#39;373&#39;, &#39;377&#39;],
    [&#39;02M418&#39;, &#39;Millennium High School &#39;, &#39;140&#39;, &#39;512&#39;, &#39;554&#39;, &#39;523&#39;],
]

csv_list = [&#39;,&#39;.join(j.strip() for j in i) for i in csv_list]

with open(&#39;output.csv&#39;, &#39;w&#39;) as new_file:
    print(&#39;,&#39;.join(rows), file=new_file)
    for row in csv_list: print(row, file=new_file)

https://onlinegdb.com/yThIfUFWy

答案3

得分: 0

我实际上发现的是,对我起作用的是上面其他建议的结合。

我改变了向 csv_list 添加事物的方式:

for item in self._data_list['data']:
    if item[8] in dbn_list:
        csv_list.append(item[8:14])

这是将一个列表附加到另一个列表,而不是将一个字符串附加到列表。

然后,我去掉了 while 循环,改用了 for 循环:

for list_item in csv_list:
    if ',' in list_item[1]:
        list_item[1] = f'"{list_item[1]}"'
    new_file.write(','.join(list_item))
    new_file.write('\n')

这包括了一个我最初没有寻求帮助的额外检查。现在我的输出正是我想要的。

英文:

So what I actually found is that it's a combination of the other recommendations above that worked for me.

I changed the way I added things to csv_list:

        for item in self._data_list[&#39;data&#39;]:
        if item[8] in dbn_list:
            csv_list.append(item[8:14])

This is appending a list to the list, rather than a string to the list.

then, I got rid of the while loop and used a for loop instead:

            for list_item in csv_list:
            if &#39;,&#39; in list_item[1]:
                list_item[1] = f&#39;&quot;{list_item[1]}&quot;&#39;
            new_file.write(&#39;,&#39;.join(list_item))
            new_file.write(&#39;\n&#39;)

which includes an additional check that I didn't initially seek help with. And now my output is exactly what I want it to be.

huangapple
  • 本文由 发表于 2023年7月14日 00:00:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定