99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python 3.x讀寫csv文件中數(shù)字的方法示例
Python 3.x讀寫csv文件中數(shù)字的方法示例
2017-09-28
收藏

Python 3.x讀寫csv文件中數(shù)字的方法示例

本文主要給大家介紹了關(guān)于Python3.x讀寫csv文件中數(shù)字的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
讀寫csv文件
讀文件時先產(chǎn)生str的列表,把最后的換行符刪掉;然后一個個str轉(zhuǎn)換成int
    
## 讀寫csv文件
csv_file = 'datas.csv'
csv = open(csv_file,'w')
for i in range(1,20):
 csv.write(str(i) + ',')
 if i % 10 == 0:
  csv.write('\n')
csv.close()
result = []
with open(csv_file,'r') as f:
 for line in f:
  linelist = line.split(',')
  linelist.pop()# delete: \n
  for index, item in enumerate(linelist):
   result.append(int(item))
print('\nResult is \n' , result)

輸出:
    
Result is
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

檢查目錄是否存在

若目標(biāo)目錄不存在,則新建一個目錄
    
import os
json_dir = "../dir_json/2017-04/"
if not os.path.exists(json_dir):
 print("json dir not found")
 os.makedirs(json_dir)
 print("Create dir " + json_dir)

寫文件時指定格式

參考下面的代碼,打開文件時指定utf8,轉(zhuǎn)換成json時指定ensure_ascii=False    
import json
json_file = open(json_dir + id + '.json', 'w', encoding='utf8')
json_file.write(json.dumps(data_dict, ensure_ascii=False))

避免寫成的json文件亂碼

函數(shù) enumerate(iterable, start=0)

返回一個enumerate對象。iterable必須是一個句子,迭代器或者支持迭代的對象。

enumerate示例1:
    
>>> data = [1,2,3]
>>> for i, item in enumerate(data):
 print(i,item)
0 1
1 2
2 3

示例2:
    
>>> line = 'one'
>>> for i, item in enumerate(line,4):
 print(i,item)
4 o
5 n
6 e

參考: https://docs.python.org/3/library/functions.html?highlight=enumerate#enumerate

class int(x=0)

class int(x, base=10)

返回一個Integer對象。對于浮點數(shù),會截取成整數(shù)。    
>>> print(int('-100'),int('0'),int('3'))
-100 0 3
>>> int(7788)
7788
>>> int(7.98)
7
>>> int('2.33')
Traceback (most recent call last):
 File "<pyshell#27>", line 1, in <module>
 int('2.33')
ValueError: invalid literal for int() with base 10: '2.33'

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }