英文:
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
"""import json module"""
class SatData:
"""reads a json files and writes it to a CSV file"""
def __init__(self):
try:
with open('sat.json', 'r') as json_file:
self._data_list = json.load(json_file)
except FileNotFoundError:
print('file not found')
def save_as_csv(self, dbn_list):
csv_list = []
rows = ['DBN', 'School Name', 'Number of Test Takers',
'Critical Reading Mean', 'Mathematics Mean', 'Writing Mean']
for item in self._data_list['data']:
if item[8] in dbn_list:
csv_list.append(str(item[8:14]))
sorted(csv_list)
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)
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
['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']
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 = ','
that just gives me output with each individual character separated by a comma
','.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(','.join(item[8:14]))
, or you could use list comprehension to remove the spacing: csv_list.append(','.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 = ['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)
答案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['data']:
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 ',' in list_item[1]:
list_item[1] = f'"{list_item[1]}"'
new_file.write(','.join(list_item))
new_file.write('\n')
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论