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

How to modify iTunes backup path under Windows

0. Preparation: • Close iTunes • Kill the service...

Linux system file sharing samba configuration tutorial

Table of contents Uninstall and install samba Cre...

Detailed explanation of the principle and usage of MySQL views

This article uses examples to illustrate the prin...

Detailed example of using the distinct method in MySQL

A distinct Meaning: distinct is used to query the...

Summary of JavaScript Timer Types

Table of contents 1.setInterval() 2.setTimeout() ...

Some suggestions for improving Nginx performance

If your web application runs on only one machine,...

Summary of the benefits of deploying MySQL delayed slaves

Preface The master-slave replication relationship...

Forever+nginx deployment method example of Node site

I recently bought the cheapest Tencent cloud serv...

Mini Program to Implement Slider Effect

This article example shares the specific code for...

Use IISMonitor to monitor web pages and automatically restart IIS

Table of contents 1. Tool Introduction 2. Workflo...

How to install MySQL and MariaDB in Docker

Relationship between MySQL and MariaDB MariaDB da...

Using loops in awk

Let's learn about different types of loops th...

Introduction to Docker Architecture

Docker includes three basic concepts: Image: A Do...

Solve the problem of docker log mounting

The key is that the local server does not have wr...