导航:首页 > 编程语言 > 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列表循环读取相关的资料

热点内容
精品源码怎么算 浏览:490
加密技术在现实中应用体会 浏览:180
单片机如何换晶振 浏览:206
合并两个数组java 浏览:13
命令标注圆半径怎么用 浏览:659
出差人员电脑加密 浏览:248
文件夹0项什么意思 浏览:602
空调冷媒压缩机启动 浏览:911
流氓文件夹怎么强制删除 浏览:783
嵌入式基础命令 浏览:526
当程序员必学的编程语言 浏览:907
pro文件夹有什么用 浏览:357
我的世界服务器有人开挂如何踢出 浏览:482
程序员有天赋 浏览:640
编程计算并输出半径 浏览:709
日期排序java 浏览:953
程序员的中介模式 浏览:881
python中求两个字符串重复部分 浏览:495
如何查看收件发件服务器 浏览:110
linux配置android环境变量 浏览:132