read
import requests
import csv

Url = 'http://xxxx/aaaa.txt'
SaveFileName = 'data/aaa.txt'

def downLoadTourH(saveFile):
    r = requests.get(url, allow_redirects=True)
    open(saveFile, 'wb').write(r.content)

def loadCsvFromTourH():

    with open(TourGoodsFileName, newline='\n',encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile,dialect='excel-tab')
		#reader = csv.DictReader(csvfile, delimiter='\t')
        for row in reader:
            print(row['id'], row['region1'])


downLoadTourH(SaveFileName)


loadCsvFromTourH()
         

write
import csv


def SomeInfoJsonToCSV(filename,jsonData):            
    with open(filename, 'w') as csvfile:
        fieldnames = ['gid','gkey','name','latitude','longitude','region1','region2',
                                     'address','lowestRate','highestRate','mainProvider' ]
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames,lineterminator='\n')
        writer.writeheader()
        for key in jsonData:
            try:
                info = jsonData[key]                        
                someInfo = {'gid':info['gid'],'gkey':info['gkey'],'name':info['name'],'latitude':info['latitude'],'longitude':info['longitude'],'region1':info['region1'],'region2':info['region2'],
                                         'address':info['address'],'lowestRate':info['lowestRate'],'highestRate':info['highestRate'],'mainProvider':info['mainProvider'] }            
                writer.writerow(someInfo)
            except UnicodeError:
                someInfo = {} # 인코딩에러처리                            
                writer.writerow(someInfo)




참고 : https://docs.python.org/3/library/csv.html

  • No labels