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

Detailed explanation of the use of Join in Mysql

In the previous chapters, we have learned how to ...

Solution for Docker Swarm external verification load balancing not taking effect

Problem Description I created three virtual machi...

Vue implements small notepad function

This article example shares the specific code of ...

Q&A: Differences between XML and HTML

Q: I don’t know what is the difference between xml...

A detailed introduction to the basics of Linux scripting

Table of contents 1. Script vim environment 2. Ho...

Methods and problems encountered in installing mariadb in centos under mysql

Delete the previously installed mariadb 1. Use rp...

How to implement Docker container self-start

Container auto-start Docker provides a restart po...

Ajax responseText parses json data case study

Solve the problem that the responseText returned ...

Tutorial on installing MySQL on Alibaba Cloud Centos 7.5

It seems that the mysql-sever file for installing...

Solution to the failure of 6ull to load the Linux driver module

Table of contents 0x01 Failed to load the driver ...

Docker case analysis: Building a Redis service

Table of contents 1 Create mount directories and ...

HTML uses regular expressions to test table examples

Here is an example code for using regular express...