当前位置:首页 » 编程语言 » python读取

python读取

发布时间: 2022-02-02 02:59:24

python 读取文件

#!/usr/bin/python2.7
import random,re
f0=file('proxys.txt','r')
dat0=f0.readlines()
f0.close()
#提取含有$1sec的行(我理解你想按sec的大小排序。)
dat1=[]
for i in dat0:
dat1.append((i,re.search(r'\$(\d+)sec',i).group(1)))
#现在dat1里面的数据是在原来的每一行前面加了一列sec的值。
dat2=[]
for i in dat1:
if i[0]==1:
dat2.append(i[1])
#现在取出了所有sec==1的行,随机取一行
dat3=random.choice(dat2)
c1=re.search(r'((\d{1,3}\.?){4}):(\d+)',dat3).group(1)
c2=re.search(r'((\d{1,3}\.?){4}):(\d+)',dat3).group(3)

⑵ python 怎么完整的读取文件

file=open("a.txt")
s=file.read()
print(s)

⑶ 如何读取python的.py文件

你说的环境变量其实是系统的环境变量,

进入python后,还要配置python自己的环境变量。

首先,你应该进入C:Usersqc后,再启动python

cdC:Usersqc
python
>>>importex25

如果从其它地方进入python:

CMD进入python后,要加入路径:

>>>importsys
>>>sys.path.insert(0,"C:\Users\qc")
>>>importex25

⑷ Python 读取文本文件,怎么才能读取一段内容

python读取段落需要自定义函数:
from _ _future_ _ import generators

def paragraphs(fileobj, separator='\n'):
if separator[-1:] != '\n': separator += '\n' paragraph = []
for line in fileobj:
if line == separator:
if paragraph: yield ''.join(paragraph)
paragraph = []
else: paragraph.append(line)
if paragraph: yield ''.join(paragraph)

⑸ python怎么读取TXT

Python的文本处理是经常碰到的一个问题,Python的文本文件的内容读取中,有三类方法:read()、readline()、readlines(),这三种方法各有利弊,下面逐一介绍其使用方法和利弊。

read():

  • read()是最简单的一种方法,一次性读取文件的所有内容放在一个大字符串中,即存在内存中

    file_object = open('test.txt') //不要把open放在try中,以防止打开失败,那么就不用关闭了try:
    file_context = file_object.read() //file_context是一个string,读取完后,就失去了对test.txt的文件引用
    # file_context = open(file).read().splitlines()
    // file_context是一个list,每行文本内容是list中的一个元素finally:
    file_object.close()//除了以上方法,也可用with、contextlib都可以打开文件,且自动关闭文件,//以防止打开的文件对象未关闭而占用内存
  • read()的利端:

  • 方便、简单

  • 一次性独读出文件放在一个大字符串中,速度最快

  • read()的弊端:

  • 文件过大的时候,占用内存会过大

  • readline():

  • readline()逐行读取文本,结果是一个list

    with open(file) as f: line = f.readline() while line:
    print line
    line = f.readline()
  • readline()的利端:

  • 占用内存小,逐行读取

  • readline()的弊端:

  • 由于是逐行读取,速度比较慢

  • readlines():

  • **readlines()一次性读取文本的所有内容,结果是一个list

    with open(file) as f: for line in f.readlines():
    print line
  • 这种方法读取的文本内容,每行文本末尾都会带一个' '换行符 (可以使用L.rstrip(' ')去掉换行符)

  • readlines()的利端:

  • 一次性读取文本内容,速度比较快

  • readlines()的弊端:

  • 随着文本的增大,占用内存会越来越多

  • 最简单、最快速的逐行处理文本的方法:直接for循环文件对象

  • file_object = open('test.txt','rU')try:

  • for line in file_object:

  • do_somthing_with(line)//line带" "finally:

  • file_object.close()

⑹ python读取文件内容

fname = raw_input('input filename:')

with open(fname,'r') as f:
for i in f:
print i

这样试试呢?
你的代码本身看是没问题的呢!

⑺ python 文件读取的问题

首先,"失败实验2"是能够成功的。为什么你失败了呢?我用的2.5。
也就是说,创建后,用close()将数据写入,再使用同样的变量名读取是完全没有问题的。(因为已经重新定义了嘛!)

失败的实验1中,

f.write('ase') 后数据并没有被写入,在缓存区中。
然后f.read(),我个人理解为连缓存中的其他数据也读取了,或者破坏了缓存中的内容。可以看到,开头是要写入的内容‘asd’,后面是无意义的数据。

f.close()时,写入的已经是被修改后的缓存区内容了……也就是asd再加上一大串NULL等……

这个例子告诉我们……用W+时还没close前不要读取……哈哈。
恩~恩~

⑻ python如何读取文件的内容

# _*_ coding: utf-8 _*_

import pandas as pd

# 获取文件的内容

def get_contends(path):

with open(path) as file_object:

contends = file_object.read()

return contends

# 将一行内容变成数组

def get_contends_arr(contends):

contends_arr_new = []

contends_arr = str(contends).split(']')

for i in range(len(contends_arr)):

if (contends_arr[i].__contains__('[')):

index = contends_arr[i].rfind('[')

temp_str = contends_arr[i][index + 1:]

if temp_str.__contains__('"'):

contends_arr_new.append(temp_str.replace('"', ''))

# print(index)

# print(contends_arr[i])

return contends_arr_new

if __name__ == '__main__':

path = 'event.txt'

contends = get_contends(path)

contends_arr = get_contends_arr(contends)

contents = []

for content in contends_arr:

contents.append(content.split(','))

df = pd.DataFrame(contents, columns=['shelf_code', 'robotid', 'event', 'time'])

(8)python读取扩展阅读:

python控制语句

1、if语句,当条件成立时运行语句块。经常与else, elif(相当于else if) 配合使用。

2、for语句,遍历列表、字符串、字典、集合等迭代器,依次处理迭代器中的每个元素。

3、while语句,当条件为真时,循环运行语句块。

4、try语句,与except,finally配合使用处理在程序运行中出现的异常情况。

5、class语句,用于定义类型。

6、def语句,用于定义函数和类型的方法。

⑼ Python如何从文件读取数据

1.1 读取整个文件

要读取文件,需要一个包含几行文本的文件(文件PI_DESC.txt与file_reader.py在同一目录下)

PI_DESC.txt

3.1415926535
8979323846
2643383279
5028841971

file_reader.py

with open("PI_DESC.txt") as file_object:
contents = file_object.read()
print(contents)

我们可以看出,读取文件时,并没有使用colse()方法,那么未妥善的关闭文件,会不会导致文件收到损坏呢?在这里是不会的,因为我们在open()方法前边引入了关键字with,该关键字的作用是:在不需要访问文件后将其关闭

1.2文件路径

程序在读取文本文件的时候,如果不给定路径,那么它会先在当前目录下进行检索,有时候我们需要读取其他文件夹中的路径,例如:

⑽ python 文件读写

你的filename是多少,看提示貌似是你的文件不存在活着路径错误

热点内容
lua编程pdf 发布:2024-05-03 01:39:26 浏览:24
脐带血存储的三大忌 发布:2024-05-03 01:32:38 浏览:934
宝马可编程 发布:2024-05-03 01:32:20 浏览:226
匿名ftp的作用 发布:2024-05-03 01:30:13 浏览:420
php支付程序 发布:2024-05-03 01:07:09 浏览:287
安卓插口是什么型号图片 发布:2024-05-03 01:01:57 浏览:253
移动android开发 发布:2024-05-03 00:26:42 浏览:628
安卓上的字体怎么放大 发布:2024-05-03 00:26:38 浏览:304
奥迪a3低配中配推荐哪个配置 发布:2024-05-03 00:22:12 浏览:242
python类声明变量类型 发布:2024-05-03 00:22:05 浏览:641