導航:首頁 > 編程語言 > python列表循環讀取

python列表循環讀取

發布時間:2023-07-14 11:22:28

『壹』 python+selenium怎麼讀取csv中的數據進行列表循環登錄自動化參數登錄

為了參數化csv文件中的數據,需要做到兩點:
一是逐行讀取:用到列表
二是根據列名獲取對應單元格的值:用到字典

import csv

bid_info = csv.DictReader(open('bid_info.csv','r'))
dict_data = []
for lines in bid_info:
if bid_info.line_num == 1:
continue
else:
dict_data.append(lines)
row_num = len(dict_data)
# print('this is all the data---' + str(dict))

#循環讀取每一行
i = 0
while(i < row_num):
print('this is'+str(i)+'row----'+ str(dict_data[i]))
print(dict_data[i]['a'])
i += 1

『貳』 Python如何用列表的數據循環處理

L = ['地區1', '塵燃培地區2', '地區3', '地區4', '地區5', '地區派唯6', '地區段廳7']

for val in L:
# 進行操作邏輯
print(val)

『叄』 python循環怎麼用多線程去運行

背景:Python腳本:讀取文件中每行,放入列表中;循環讀取列表中的每個元素,並做處理操作。
核心:多線程處理單個for循環函數調用
模塊:threading
第一部分:

:多線程腳本 (該腳本只有兩個線程,t1循環次數<t2)#!/usr/bin/env python#-*- coding: utf8 -*- import sysimport timeimport stringimport threadingimport datetimefileinfo = sys.argv[1] # 讀取文件內容放入列表host_list = []port_list = [] # 定義函數:讀取文件內容放入列表中def CreateList(): f = file(fileinfo,'r') for line in f.readlines(): host_list.append(line.split(' ')[0]) port_list.append(line.split(' ')[1]) return host_list return port_list f.close() # 單線程 循環函數,注釋掉了#def CreateInfo(): # for i in range(0,len(host_list)): # 單線程:直接循環列表# time.sleep(1)# TimeMark = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')# print "The Server's HostName is %-15s and Port is %-4d !!! [%s]" % (host_list[i],int(port_list[i]),TimeMark)# # 定義多線程循環調用函數def MainRange(start,stop): #提供列表index起始位置參數 for i in range(start,stop): time.sleep(1) TimeMark = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') print "The Server's HostName is %-15s and Port is %-4d !!! [%s]" % (host_list[i],int(port_list[i]),TimeMark) # 執行函數,生成列表CreateList()# 列表分割成:兩部分 mid為列表的index中間位置mid = int(len(host_list)/2) # 多線程部分threads = []t1 = threading.Thread(target=MainRange,args=(0,mid))threads.append(t1)t2 = threading.Thread(target=MainRange,args=(mid,len(host_list)))threads.append(t2) for t in threads: t.setDaemon(True) t.start()t.join()print "ok"

以上是腳本內容!!!
----------------------------------------------------------------------
:讀取文件的內容
文件內容:
[root@monitor2 logdb]# cat hostinfo.txt
192.168.10.11 1011
192.168.10.12 1012
192.168.10.13 1013
192.168.10.14 1014
192.168.10.15 1015
192.168.10.16 1016
192.168.10.17 1017
192.168.10.18 1018
192.168.10.19 1019
192.168.10.20 1020
192.168.10.21 1021
192.168.10.22 1022
192.168.10.23 1023
192.168.10.24 1024
192.168.10.25 1025

:輸出結果:
單線程 : 執行腳本:輸出結果:
[root@monitor2 logdb]# ./Threadfor.py hostinfo.txt
The Server's HostName is 192.168.10.10 and Port is 1010 !!! [2017-01-10 14:25:14]
The Server's HostName is 192.168.10.11 and Port is 1011 !!! [2017-01-10 14:25:15]
The Server's HostName is 192.168.10.12 and Port is 1012 !!! [2017-01-10 14:25:16]
.
.
.
The Server's HostName is 192.168.10.25 and Port is 1025 !!! [2017-01-10 14:25:29]

多線程:執行腳本:輸出 結果
[root@monitor2 logdb]# ./Threadfor.py hostinfo.txt
The Server's HostName is 192.168.10.11 and Port is 1011 !!! [2017-01-10 14:51:51]
The Server's HostName is 192.168.10.18 and Port is 1018 !!! [2017-01-10 14:51:51]
The Server's HostName is 192.168.10.12 and Port is 1012 !!! [2017-01-10 14:51:52]
The Server's HostName is 192.168.10.19 and Port is 1019 !!! [2017-01-10 14:51:52]
The Server's HostName is 192.168.10.13 and Port is 1013 !!! [2017-01-10 14:51:53]
The Server's HostName is 192.168.10.20 and Port is 1020 !!! [2017-01-10 14:51:53]
The Server's HostName is 192.168.10.14 and Port is 1014 !!! [2017-01-10 14:51:54]
The Server's HostName is 192.168.10.21 and Port is 1021 !!! [2017-01-10 14:51:54]
The Server's HostName is 192.168.10.15 and Port is 1015 !!! [2017-01-10 14:51:55]
The Server's HostName is 192.168.10.22 and Port is 1022 !!! [2017-01-10 14:51:55]
The Server's HostName is 192.168.10.16 and Port is 1016 !!! [2017-01-10 14:51:56]
The Server's HostName is 192.168.10.23 and Port is 1023 !!! [2017-01-10 14:51:56]
The Server's HostName is 192.168.10.17 and Port is 1017 !!! [2017-01-10 14:51:57]
The Server's HostName is 192.168.10.24 and Port is 1024 !!! [2017-01-10 14:51:57]
The Server's HostName is 192.168.10.25 and Port is 1025 !!! [2017-01-10 14:51:58]

『肆』 Python列表嵌套多個字典,循環讀取字典「名稱」,並輸出

cloris={'Owner':'jack','kind':'dog'}
brinkley={'Owner':'tom','kind':'cat'}
pets=['cloris','brinkley']
foriinpets:
print('%s:'%i)
print('Owner:%s,kind:%s'%(eval(i)['Owner'],eval(i)['kind']))

驀然回首···還是這個好看

閱讀全文

與python列表循環讀取相關的資料

熱點內容
硬碟格式化時用的dos命令是 瀏覽:937
找人緩解壓力 瀏覽:929
iphone的pdf 瀏覽:342
90壓縮餅干怎麼吃 瀏覽:660
php教材下載 瀏覽:906
什麼解壓密碼最好 瀏覽:582
資料庫與伺服器如何連接 瀏覽:436
架構師需要閱讀的源碼 瀏覽:475
ch編譯器 瀏覽:450
java必須自己寫一個編譯器嗎 瀏覽:938
如何製作androidrom 瀏覽:470
單片機萬能板怎麼寫入程序 瀏覽:21
邁銳寶xl壓縮比 瀏覽:340
靠演算法買彩票 瀏覽:497
程序員考核d 瀏覽:239
自助游中國pdf 瀏覽:746
安卓p40是什麼手機 瀏覽:87
24cxx編程器 瀏覽:591
陰陽師如何查看哪個伺服器有ID 瀏覽:316
公務員照片壓縮 瀏覽:458