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

Apache ab concurrent load stress test implementation method

ab command principle Apache's ab command simu...

Detailed explanation of jQuery's copy object

<!DOCTYPE html> <html lang="en"...

Implementation method of Mysql tree recursive query

Preface For tree-structured data in the database,...

10 SQL statement optimization techniques to improve MYSQL query efficiency

The execution efficiency of MySQL database has a ...

A brief discussion on HTML doctype and encoding

DOCTYPE Doctype is used to tell the browser which...

Summary of essential Docker commands for developers

Table of contents Introduction to Docker Docker e...

Detailed explanation of Docker compose orchestration tool

Docker Compose Docker Compose is a tool for defin...

How to implement JavaScript's new operator yourself

Table of contents Constructor new Operator Implem...

How to use Linux whatis command

01. Command Overview The whatis command searches ...

Detailed explanation of various join summaries of SQL

SQL Left Join, Right Join, Inner Join, and Natura...

Use the more, less, and cat commands in Linux to view file contents

In Linux, the commands cat, more, and less can al...

Detailed explanation of MySQL foreign key constraints

Official documentation: https://dev.mysql.com/doc...

How to install JDK and Mysql on Ubuntu 18.04 Linux system

Platform deployment 1. Install JDK step1. Downloa...