简体   繁体   English

如何迭代列表并将其写为CSV标头?

[英]How to iterate and write a list as the CSV header?

I have a use case, where I pull the data from a SPARQL endpoint as follows: 我有一个用例,其中我从SPARQL端点提取数据,如下所示:

SPARQL = SPARQLWrapper(endpointURL)  
SPARQL.setQuery(queryString) 
SPARQL.setReturnFormat(JSON) 
results = SPARQL.query().convert() 

The 'results' variable holds the data that now I want to write in a CSV file. “结果”变量保存了现在我要写入CSV文件中的数据。 First, I created the header for the CSV as follows: 首先,我为CSV创建了标头,如下所示:

schema = ['Age', 'Sex', 'Chest_Pain_Type', 'Trestbps', 'Chol', 'Fasting_Glucose_Level', 'Resting_ECG_Type', 'ThalachD', 'Exercise_Induced_Angina', 'OldpeakD', 'CaD', 'Slope', 'Thallium_Scintigraphy', 'Diagnosis']

I then wrote the data of each row in a CSV file using the following code: 然后,我使用以下代码将每一行的数据写入CSV文件:

with open('DIC.csv', 'w+') as csvfile: 
        writer = csv.writer(csvfile, delimiter=',') 
        writer.writerow([g for g in schema]) 
        for result in results["results"]["bindings"]: 
                    writer.writerow([result["Age"]["value"], 
                    result["SexTypes"]["value"],
                    result["Chest_Pain_Type"]["value"], 
                    result["trestbpsD"]["value"],
                    result["cholD"]["value"], 
                    result["Fasting_Glucose_Level"]["value"],
                    result["Resting_ECG_Type"]["value"],
                    result["thalachD"]["value"],
                    result["Exercise_Induced_Angina"]["value"],
                    result["oldpeakD"]["value"],
                    result["caD"]["value"],
                    result["Slope"]["value"],
                    result["Thallium_Scintigraphy"]["value"],
                    result["Diagnosis"]["value"]])
        file_name = csvfile.name
        csvfile.close()

In the preceding code block, result["SexTypes"]["value"] is used to write the value (ie "value") of column "SexTypes". 在前面的代码块中,使用result [“ SexTypes”] [“ value”]写入列“ SexTypes”的值(即“ value”)。 That means, the first index is variable but the second index is always the same. 也就是说,第一个索引是可变的,但是第二个索引始终是相同的。

Although, the above code works fine but this is pretty much hard-coded and fails once my SPARQL query changes(ie if the schema is different). 虽然,上面的代码可以正常工作,但是这几乎是硬编码的,一旦我的SPARQL查询更改(即,如果架构不同),它就会失败。

I now want to make it more flexible such that I iterate all the column from the list but the "value" is fixed. 我现在想使其更加灵活,这样我可以迭代列表中的所有列,但“值”是固定的。 For doing this, I tried with the following code, which eventually fails: 为此,我尝试了以下代码,但最终失败了:

with open('DIC.csv', 'w+') as csvfile: 
        writer = csv.writer(csvfile, delimiter=',') 
        writer.writerow([g for g in schema]) 
        r = "value"
        for result in results["results"]["bindings"]: 
            for i in range(len(schema)):
                writer.writerow([result[schema[i]][r]])
                i = i + 1       
        file_name = csvfile.name
        csvfile.close()
        return file_name

I know, I'm doing somethig wrong. 我知道,我做错了什么。 Any better suggestion, please? 有什么更好的建议吗?

[Maybe the problem deserves a better title but I couldn't find any. [也许这个问题应该得到更好的称呼,但我找不到。 Sorry for my bad English, though.] 抱歉,我的英语不好。]

The input of writer.writerow() in the first example code is a list, with the same length as schema , whereas in the second it is a length 1 list (and instead of calling writerow() once you call is once per element in schema . 第一个示例代码中writer.writerow()的输入是一个列表,其长度与schema相同,而在第二个示例代码中,它是一个长度为1的列表(并且一次调用一次,而不是调用writerow()schema

To generate the list you had in the first place you can do it with a list comprehension: 首先,要生成列表,可以使用列表理解功能:

row = [result[column]["value"] for column in schema]

which is equivalent to: 等效于:

row = [] # create empty list
for column in schema:
    row.append(result[column]["value"])

Eventually, the code would only have to be modified inside the loop over results : 最终,只需要在循环results内部修改代码即可:

...
for result in results["results"]["bindings"]: 
    row = [result[column]["value"] for column in schema]
    writer.writerow(row)
file_name = csvfile.name
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM