简体   繁体   English

Python 2.7.10 CSV如何在新行中写入迭代输出的每个版本

[英]Python 2.7.10 csv how to write each version of an iterating output in a new row

I want to write an iterating output, with each version in a new row of the csv. 我想编写一个迭代输出,每个版本都放在csv的新行中。 I have tried this: 我已经试过了:

#matching questions
for ind1,key1 in enumerate(file1):
    for ind2,key2 in enumerate(file2):
        ques1 = file1[key1]
        ques2 = file2[key2]
        match_ratio = difflib.SequenceMatcher (None, ques1, ques2).ratio()
        n = csv.writer(open("output.csv", "wb"))
        n.writerow([key1, ques1, key2, ques2, match_ratio])

As you can see I am trying to match two strings each from a different dictionary and then I want to output the key and value of each and the match ratio into a csv file so I can work with the output in excel. 如您所见,我试图匹配来自不同词典的两个字符串,然后将每个字符串的键和值以及匹配率输出到一个csv文件中,以便可以在excel中使用输出。 However, only the last versions of each variable is getting output and I have no clue why. 但是,只有每个变量的最新版本都可以输出,我也不知道为什么。

Why is it not outputting the variables into a new row in the csv each time? 为什么每次都不将变量输出到csv中的新行中? What should I do to make this happen? 我应该怎么做才能做到这一点?

Thanks in advance! 提前致谢!

您的代码将在每次迭代时重写数据,而是以追加模式打开文件。

As @amal-ts said, you should use append mode to open file. 正如@ amal-ts所说,您应该使用附加模式打开文件。

And opening file in every loop is bad idea, so you should consider using with 在每个循环中打开文件是个坏主意,因此您应该考虑使用with

with open("output.csv", "ab") as f:
    for ind1,key1 in enumerate(file1):
        for ind2,key2 in enumerate(file2):
            ques1 = file1[key1]
            ques2 = file2[key2]
            match_ratio = difflib.SequenceMatcher (None, ques1, ques2).ratio()
            n = csv.writer(f)
            n.writerow([key1, ques1, key2, ques2, match_ratio])

Finally, you should check match_ratio value to check your blank row problem. 最后,您应该检查match_ratio值以检查您的空白行问题。

暂无
暂无

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

相关问题 在 Python 中,通过测试运行 csv 中的每一行,并在 output 中运行一个新的 Z628CB5675FF524F3EZE719 - In Python, run each row in a csv through tests and output a new csv showing which test each row failed Python:当一个键有多个值时,如何将字典写入csv文件,每个键是一个新行,但每个值是一个新列? - Python: How to write dictionary to csv file when one key has multiple values, each key is a new row, but each value is a new column? 如何将列表写入csv,每个项目都在新行上 - How to write list to csv, with each item on a new row 使用 python,如何将新项目写入 CSV 文件中的新行 - Using python, how to write new item into a new row in a CSV file 如何编写和读取每行的csv文件python - How to write and read csv file each row python Python - 将同一标题下的每个列表数据的新行写入csv - Python - Write a new row for each list data under same header into csv 如何在csv文件的每一行中写入一个值? - How to write a value in each row of a csv file? 如何通过遍历 Python 中的 dataframe 中的每一行来将计算值存储在新列中? - How to store a calculated value in new column by iterating through each row in a dataframe in Python? 如果row [index] ==“ String” python,则将行写入新的csv - Write row to new csv if row[index] == “String” python 如何将字符串列表写入 CSV 文件,列表中的每一项都在一个新行中? - How do I write a list of strings to a CSV file, with each item of the list on a new row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM