Python writes output to csv operation

Python writes output to csv operation

As shown below:

 def test_write(self):
 fields=[]
 fields.append(orderCode)
 with open(r'./test001.csv', 'a',newline="") as f:
  writer = csv.writer(f)
  writer.writerow(fields)

Define a list: Then add the data to be written to the list

a #Append data to the next line

newline='' //Remove the blank line in the middle of each line. If not added, there will be spaces between lines.

Add newline

Without:

Supplement: Use python to convert json data into csv format

Assume that the data stored in the .json file is:

{"type": "Point", "link": "http://www.dianping.com/newhotel/22416995", "coordinates": [116.37256372996957, 40.39798447055443], "category": "Economy", "name": "Beijing Hetang Villa", "count": "278", "address": "Ansi Road, Huanghuacheng Village, Huairou District, Beijing", "price": "380"}
{"type": "Point", "link": "http://www.dianping.com/newhotel/19717653", "coordinates": [116.56881588256466, 40.43310967948417], "category": "Economy", "name": "Mutianyu Great Wall Yushifu Country Hotel", "count": "89", "address": "Weidian Village, Bohai Town, Huairou District, Beijing (3 kilometers below Mutianyu Great Wall, near Huaihuang Road)", "price": "258"}
{"type": "Point", "link": "http://www.dianping.com/newhotel/58365289", "coordinates": [116.62874974822378, 40.45610264855833], "category": "Economy", "name": "Beijing Mitaoer Parent-child Inn", "count": "119", "address": "No. 11 Xiaguandi, Shentangyu Scenic Area, Huairou District, Beijing", "price": "549"}

Now we need to save the above data in csv format, where the keys of the dictionary are the attribute names in csv, and the values ​​of the dictionary are the values ​​corresponding to the attributes in csv.

If you only need to generate csv according to the keys of json, the operation is relatively simple, just follow the method below:

#-*-coding:utf-8-*-
import csv
import json
import sys
import codecs
def trans(path):
  jsonData = codecs.open(path+'.json', 'r', 'utf-8')
  # csvfile = open(path+'.csv', 'w') # Writing here will cause the written file to have blank lines # csvfile = open(path+'.csv', 'wb') # python2 under csvfile = open(path+'.csv', 'w', newline='') # python3 under writer = csv.writer(csvfile, delimiter='\t', quoting=csv.QUOTE_ALL)
  flag = True
  for line in jsonData:
    dic = json.loads(line[0:-1])
    if flag:
      # Get the attribute list keys = list(dic.keys())
      print (keys)
      writer.writerow(keys) # Write the attribute list to csv flag = False
    # Read each line of json data and write the values ​​data to csv one line at a time writer.writerow(list(dic.values()))
  jsonData.close()
  csvfile.close()
if __name__ == '__main__':
  path=str(sys.argv[1]) # Get the path parameter print (path)
  trans(path)

Run under python3, enter the command line

python C:\Users\MaMQ\Documents\jsonToCsv.py C:\Users\MaMQ\Documents\data\geoFood

The third parameter is the path and name of the file to be converted, with the suffix deleted. After running the file, you can get the converted csv file.

If you need to modify the key field of each dictionary in the json file, for example, you need to take out the longitude and latitude data in the coordinate in the above dict and save it as x and y data, you can follow the following method (this method can also adjust the order in which each attribute is displayed, which is better):

import csv
import json
import sys
import codecs
def trans(path):
  jsonData = codecs.open(path+'.json', 'r', 'utf-8')
  # csvfile = open(path+'.csv', 'w') # Writing here will cause the written file to have blank lines # csvfile = open(path+'.csv', 'wb') # python2 under csvfile = open(path+'.csv', 'w', newline='') # python3 under writer = csv.writer(csvfile, delimiter='\t', quoting=csv.QUOTE_ALL)
  keys=['id', 'name', 'category', 'price', 'count', 'type', 'address', 'link', 'x', 'y']
  writer.writerow(keys)
  i = 1
  for dic in jsonData:
    dic = json.loads(dic[0:-1])
    x = dic['coordinates'][0]
    y = dic['coordinates'][1]
    writer.writerow([str(i),dic['name'],dic['category'],dic['price'],dic['count'],dic['type'],dic['address'],dic['link'],x,y])
    i += 1
  jsonData.close()
  csvfile.close()
if __name__ == '__main__':
  path = str(sys.argv[1])
  print (path)
  trans(path)

The operation method is the same as above.

The json file is the data I crawled from Dianping.com, and the storage format is utf-8. It is recommended to use the codecs package to read json data, and you can specify the encoding method.

jsonData = codecs.open(path + '.json', 'r', encoding='utf-8')

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • How to write data to a CSV file using Python
  • 3 ways to write data to csv or xlsx files in python
  • How to output classification_report to csv file in python

<<:  Quickjs encapsulates JavaScript sandbox details

>>:  Understand CSS3 FlexBox elastic layout in 10 minutes

Recommend

Convert XHTML CSS pages to printer pages

<br />In the past, creating a printer-friend...

Five guidelines to help you write maintainable CSS code

1. Add a comment block at the beginning of the sty...

How to add a disk in Vmware: Expand the disk

This article describes how to add or expand a dis...

Zabbix monitors the process of Linux system services

Zabbix automatically discovers rules to monitor s...

13 Most Frequently Asked Vue Modifiers in Interviews

Table of contents 1. lazy 2.trim 3.number 4.stop ...

Weather icon animation effect implemented by CSS3

Achieve results Implementation Code html <div ...

Detailed explanation of Navicat's slow remote connection to MySQL

The final solution is in the last picture If you ...

JavaScript macrotasks and microtasks

Macrotasks and Microtasks JavaScript is a single-...

JS realizes simple picture carousel effect

This article shares the specific code of JS to ac...

How to remove spaces or specified characters in a string in Shell

There are many methods on the Internet that, alth...

Detailed explanation of the practical application of centos7 esxi6.7 template

1. Create a centos7.6 system and optimize the sys...

Navicat for MySQL 15 Registration and Activation Detailed Tutorial

1. Download Navicat for MySQL 15 https://www.navi...

Detailed explanation of the use of Refs in React's three major attributes

Table of contents Class Component Functional Comp...

vue cli3 implements the steps of packaging by environment

The vue project built with cli3 is known as a zer...