Print values only in list of tuples to csv file

MarkS

I have a small program that works, but not quite what I want.

The top half works fine.

The write (bottom) half does work, but all I am interested in is the values in the tuples. Nothing else.

My code:

import json
import csv

jsonfile = "f:\mark\python\data.json"

with open(jsonfile, "r") as jfile:
    jfile_decode = json.load(jfile)
    # Create a list of tuples with a list comprehension
    jlist = [[(k, v) for k, v in d.items() if k in ['city', 'state', 'population', 'rank']] for d in jfile_decode]
    # print(jlist)  - Just for testing to make sure the output is what I wanted -


outputfile = "f:\mark\python\data.csv"
with open(outputfile, 'w') as f:
    csv_out = csv.writer(f, delimiter=' ')
    csv_out.writerow(['city', 'state', 'population', 'rank'])
    for row in jlist:
        csv_out.writerow(row)

The output I am currently getting:

city state population rank

"('city', 'New York')" "('population', '8405837')" "('rank', '1')" "('state', 'New York')"

"('city', 'Los Angeles')" "('population', '3884307')" "('rank', '2')" "('state', 'California')"

"('city', 'Chicago')" "('population', '2718782')" "('rank', '3')" "('state', 'Illinois')"

"('city', 'Houston')" "('population', '2195914')" "('rank', '4')" "('state', 'Texas')"

What I want is this: (Just the tuple values. Nothing else)

city state population rank

'New York' 'New York' '8405837' '1'

'Los Angeles' 'California' '3884307' '2'
Damián Rafael Lattenero

You can eval the string, and get the element you want:

outputfile = "f:\mark\python\data.csv"
with open(outputfile, 'w') as f:
    csv_out = csv.writer(f, delimiter=' ')
    csv_out.writerow(['city', 'state', 'population', 'rank'])
    for row in jlist:
        csv_out.writerow(" ".join([col[1] for col in row]))

Edit To erase the double quotes inside the strings use .strip('!"'):

print(" ".join([col[1].strip('!"') for col in row]))

an example:

big_tuple = [{'city': 'New York', 'growth_from_2000_to_2013': '4.8%', 'latitude': 40.7127837, 'longitude': -74.0059413, 'population': '8405837', 'rank': '1', 'state': 'New York'},{'city': 'Los Angeles', 'growth_from_2000_to_2013': '4.8%', 'latitude': 34.0522342, 'longitude': -118.2436849, 'population': '3884307', 'rank': '2', 'state': 'California'}]

jlist = [[(k, v) for k, v in d.items() if k in ['city', 'state', 'population', 'rank']] for d in big_tuple]

for row in jlist:
    print(" ".join([col[1] for col in row]))

as result

New York 8405837 1 New York 
Los Angeles 3884307 2 California

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Writing list of tuples as a CSV file

From Dev

Print lines in a csv file if the values are consecutive

From Dev

Loading data from a csv file and display in list of tuples

From Dev

Why does it not print a list of tuples with no repeated tuples?

From Dev

Why does it not print a list of tuples with no repeated tuples?

From Dev

Print out indexes of same values in file/list

From Dev

writing a file to a list of tuples

From Dev

Read file as a list of tuples

From Dev

converting a csv file headers and values into a list

From Dev

Search a list of values in a csv file in python

From Dev

Writing nested list values to CSV file

From Dev

Dropping values from a list of tuples

From Dev

How to compare values in tuples in a list

From Dev

Conditionally sum values in a list of tuples

From Dev

Conditionally sum values in a list of tuples

From Dev

How to print only the duplicate values from a text file?

From Dev

How do I print the next element in a linked list to a CSV file?

From Dev

Python3 Uniquify list of tuples based on only one of the values of the index

From Dev

Python dict of lists of tuples. print list of element from tuples

From Dev

How to print a list of tuples with no brackets in Python

From Dev

print list of tuples without brackets python

From Dev

How to print a list of tuples with no brackets in Python

From Dev

how to print message for average of tuples in a list

From Dev

Convert Map with list values to list of tuples

From Dev

Print csv file in unix

From Dev

Print dictionary of list values

From Dev

Remove leading zeros in csv file from int values only

From Dev

Filter a .CSV file based on the 5th column values of a file and print those records into a new file

From Dev

Remove repeating tuples from a list, depending on the values in the tuples

Related Related

  1. 1

    Writing list of tuples as a CSV file

  2. 2

    Print lines in a csv file if the values are consecutive

  3. 3

    Loading data from a csv file and display in list of tuples

  4. 4

    Why does it not print a list of tuples with no repeated tuples?

  5. 5

    Why does it not print a list of tuples with no repeated tuples?

  6. 6

    Print out indexes of same values in file/list

  7. 7

    writing a file to a list of tuples

  8. 8

    Read file as a list of tuples

  9. 9

    converting a csv file headers and values into a list

  10. 10

    Search a list of values in a csv file in python

  11. 11

    Writing nested list values to CSV file

  12. 12

    Dropping values from a list of tuples

  13. 13

    How to compare values in tuples in a list

  14. 14

    Conditionally sum values in a list of tuples

  15. 15

    Conditionally sum values in a list of tuples

  16. 16

    How to print only the duplicate values from a text file?

  17. 17

    How do I print the next element in a linked list to a CSV file?

  18. 18

    Python3 Uniquify list of tuples based on only one of the values of the index

  19. 19

    Python dict of lists of tuples. print list of element from tuples

  20. 20

    How to print a list of tuples with no brackets in Python

  21. 21

    print list of tuples without brackets python

  22. 22

    How to print a list of tuples with no brackets in Python

  23. 23

    how to print message for average of tuples in a list

  24. 24

    Convert Map with list values to list of tuples

  25. 25

    Print csv file in unix

  26. 26

    Print dictionary of list values

  27. 27

    Remove leading zeros in csv file from int values only

  28. 28

    Filter a .CSV file based on the 5th column values of a file and print those records into a new file

  29. 29

    Remove repeating tuples from a list, depending on the values in the tuples

HotTag

Archive