How to output a csv file for more than one list?

Andrea

The following code allows me to print a CSV output with the first "column" for the word and the second for the frequency. However, I have a second list called B that should be output in the same file.

A=[food:2,wine:1,dog:5]
B=[cat:3,bird:2]
cnt=Counter(A)
with open("list.csv", encoding='utf-8', mode='w') as fp:
    fp.write('Word:Frequency\n')  
    for tag, count in cnt.items():  
        fp.write('{}:{}\n'.format(tag, count))

The final result should be

 1. Word:Frequency:Word2:Frequency2
 2. food:2:cat:3
 3. wine:1:bird:2
 4. dog:5

Could you help me? Thanks

Barmar

Use itertools.zip_longest() to pair up two sequences, and add filler when one is shorter than the other. You can fill with an empty tuple, so those columns will be omitted when you combine the two tuples.

Use the csv module to convert these combined tuples to rows in the file.

from collections import Counter
import csv
from itertools import zip_longest

cnt1 = Counter(A)
cnt2 = Counter(B)
with open("list.csv", encoding='utf-8', mode='w') as fp:
    csv_out = csv.writer(fp, delimiter=':')
    csv_out.writerow(['Word', 'Freq', 'Word2', 'Freq2'])
    for v1, v2 in zip_longest(cnt1.items(), cnt2.items(), fillvalue=tuple()):
        csv_out.writerow(v1 + v2)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i output more than one identical line from a csv file in python

How to redirect more than one output line into a text file?

Create csv file with more than one column

How to output more than one column

How to write more than one object result to one output text file in Java?

how to read .csv file that contains more than one value in one column

Merge more than one xml and output csv in Biztalk

How to return more than one element list?

Creating more than one row in a CSV file/Chess database creation

How to use the "|" to append more than one list to an existing one?

How to train ML model to get more than one possible output?

How can a command have more than one output?

How to loop and output the attributes of more than one object in CoffeeScript?

How to fail command if zero or more than one line in output

How can i host one html file more than one?

How to remove more than one symbol from csv

More than one writes to file

How can i create list of lists from file where every list would have more than one line?

How to merge more than one list having objects into one list with sum of one of the elements of the list in c#

Attempting to export parsed data to CSV file with Python and I can't figure out how to export more than one row

How to use list comprehention to equal to more than one variable in python?

How to shift a list one more time than before each time?

how to store values in list to more than one variable ( in python django )

How to render more than one polygon from the same list

How to apply a function to more than one list of lists in r?

How return more than one match on a list of text?

How to create ico file with more than one image

How to execute more than one maven command in bat file?

How to make a text file have more than one encoding?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive