zoukankan      html  css  js  c++  java
  • python学习-文件读写整理

    #1.open 读文本文件,open需与close()配对使用

    #coding=utf-8
    # r 可省略,默认为r  
    source
    = open("D:pythonTestdatadata01.txt","r") try: values = source.read() print values finally: source.close()
    #一行行地读文本
    
    source = open("D:pythonTestdatadata01.txt")
    lines =source.readlines()
    #通过遍历每行
    for line in lines:
        print line
    
    source.close()

    #2. 打开csv文件

    csv.reader()
    import csv
    
    def loginData():
        my_file = "D:\pythonTest\dataloginData.csv"
        datas = csv.reader(file(my_file,'rb'))
       
         return datas
    
    '''
        for data in datas:
            openUrl = data[0]
            username = data[1]
            password = data[2]
    
        return data
    '''
        

    #3.打开 csv 方法: with open('xxx.csv','rb') as f:

    #读取csv文件,文档有表头
    with open('D:pythonTestdataloginData02.csv','rb') as csvfile:
            reader = csv.DictReader(csvfile)
            urls = []
            usernames = []
            passwords= []
            for row in reader:
            #需知道列名
    if row['if_excute'] != 'if_excute': url = row['url'] username = row['username'] password = row['password'] urls.append(url) usernames.append(username) passwords.append(password) print urls print passwords print usernames
  • 相关阅读:
    List中的get(i)
    报空指针异常
    json数据请求
    springmvc中的字典表
    json的解析
    httpClient返回的数据类型,怎么弄
    java中webService
    红外遥控协议(NEC)
    很奇怪的GPIO地址
    emacs命令备忘
  • 原文地址:https://www.cnblogs.com/jilu1219/p/6771277.html
Copyright © 2011-2022 走看看