python各種ファイルの読み出し方法

6886 ワード

Json:
use_time=[]
with open(address,'r') as f: #ubuntu
    mobile = json.load(f)
    calls = mobile["transactions"][0]["calls"]
for call in calls: 
  use_time.append(str(call['use_time']))

Excel:
rawdata1=open_workbook(address)
rawdata=rawdata1.sheet_by_index(0)
for i in range(1,rawdata.nrows):
    if rawdata.cell(i,date_index).value=="": #    
        continue
    else:
        if ctype==3:  #  3,  datetime      
            date1=rawdata.cell(i,date_index).value
            date2 = xldate_as_tuple(date1,0) 
            date3=datetime(*date2)
            if "." in str(rawdata.cell(i,phone_index).value):
                phone1=str(rawdata.cell(i,phone_index).value)[:-2]  
            else:
                phone1=str(rawdata.cell(i,phone_index).value)

EXCELを書く:
Excel_file = xlwt.Workbook() 
sheet = Excel_file.add_sheet('sheet0')
header=[u'  ','  top1','  top2','  top3']
#     :
for i in range(len(header)):
    sheet.write(0,i,header[i])
#        :
for i in range(len(phonelist)):
    sheet.write(i+1,0,phonelist[i])
    sheet.write(i+1,1,dic[str(phonelist[i])])
#  EXCEL:
Excel_file.save("C:/Users/Desktop/100     xls/"+str(fileName)+".xls")

CSV:
rawdata=pd.read_csv(address,skip_blank_lines=True) #       
if 'start_time' or 'begin_time'  in rawdata.columns:
    if 'start_time' in rawdata.columns:
        start_time=rawdata['start_time']
    elif 'begin_time' in rawdata.columns:
            start_time=rawdata['begin_time']

txt:
rawdata=open(address,'r')
i=0
a=[] #c        
for line in rawdata:
    if i==1: #             
        a=line.split(',') #       
        for j in range(len(a)): #            
            if (('-' in str(a[j]))or('/' in str(a[j]))): #        
                date_index=j #        
            elif  str(a[j]).isdigit() and len(str(a[j]))>5: #                 
                phone_index=j
            else:
                pass
        break
    else:
        i+=1
i=0
for line in rawdata:#      :
    if len(line)<10: #    
        continue
    data_line=line.split(',') #txt   ','    
    if i==0:
        pass #      ,  
        i+=1
    else: #          
        start_time.append(data_line[date_index])