readlines

Py---StringIO and BytesIO 读取str

依然范特西╮ 提交于 2019-12-03 22:59:32
# StringIO和BytesIO (1)StringIO顾名思义就是在内存中读写str。(2)StringIO操作的只能是str,如果要操作二进制数据,就需要使用BytesIO。 # stringIO 比如说,这时候,你需要对获取到的数据进行操作,但是你并不想把数据写到本地硬盘上,这时候你就可以用stringIO from io import StringIO from io import BytesIO def outputstring(): return 'string \nfrom \noutputstring \nfunction' s = outputstring() # 将函数返回的数据在内存中读 sio = StringIO(s) # 可以用StringIO本身的方法 print(sio.getvalue()) # 也可以用file-like object的方法 s = sio.readlines() for i in s: print(i.strip()) # 将函数返回的数据在内存中写 sio = StringIO() sio.write(s) # 可以用StringIO本身的方法查看 s=sio.getvalue() print(s) # 如果你用file-like object的方法查看的时候,你会发现数据为空 sio = StringIO() sio

read(),readline() 和 readlines() 比较

痴心易碎 提交于 2019-12-03 18:15:25
read(),readline() 和 readlines() 比较 区别:    read() 【即 fileObject().read( [size] ) 】     从文件读取指定的字节数,如果未给定或为负则读取所有       readline() 【即 fileObject.readline( [size] ) ,[size]表示可选参数。】     从文件中一行一行地整行读取数据,如果指定了一个非负数的参数,则返回指定大小的字节数。 with open(r'../learn_file/file_to_read.txt', encoding='UTF-8', mode='r+') as fb: while True: content = fb.readline().replace('\n', '') if not content: break # print(type(content)) print(content) print(fb.name)    readlines() 【即 fileObject.readlines ( [sizeint] ) ,[sizeint] 表示可选参数】     从文件一次读取所有行并返回列表,若给定sizeint > 0,返回总和大约为sizeint字节的行 来源: https://www.cnblogs.com

How to create fake text file in Python

百般思念 提交于 2019-12-03 11:11:46
How can I create a fake file object in Python that contains text? I'm trying to write unit tests for a method that takes in a file object and retrieves the text via readlines() then do some text manipulation. Please note I can't create an actual file on the file system. The solution has to be compatible with Python 2.7.3. Dougal This is exactly what StringIO / cStringIO (renamed to io.StringIO in Python 3) is for. Joran Beasley Or you could implement it yourself pretty easily especially since all you need is readlines() : def FileSpoof: def __init__(self,my_text): self.my_text = my_text def

Python restrict newline characters for readlines()

可紊 提交于 2019-12-03 09:37:28
I am trying to split a text which uses a mix of new line characters LF , CRLF and NEL . I need the best method to exclude NEL character out of the scene. Is there an option to instruct readlines() to exlude NEL while splitting lines? I may be able to read() and go for matching only LF and CRLF split points on a loop. Is there any better solution? I open the file with codecs.open() to open utf-8 text file. And while using readlines() , it does split at NEL characters: The file contents are: "u'Line 1 \\x85 Line 1.1\\r\\nLine 2\\r\\nLine 3\\r\\n'" file.readlines() will only ever split on \n , \r

python文件操作

最后都变了- 提交于 2019-12-03 05:27:19
使用open函数可以打开文件并返回一个文件对象,返回的文件对象用来读取和写入文件内容。那么,如何使用文件对象来读取文件内容呢?如何让读取的文件内容初始化一个Python列表呢? 文本文件和二进制文件 使用文件对象读取文件内容时,要根据文件的不同存储类型选择不同的读取方式。一般来说,文件的存储类型主要分为文本文件和二进制文件两大类。文本文件就是可以用记事本打开的文件,文本文件主要存储了文字信息及换行符等控制符号,任何程序都可以打开文本文件并能正确显示文件内容;二进制文件主要是以二进制方式来存储内容,二进制文件很难被用户或其它程序理解,读取后也无法正确显示,只有创建它的程序才能够正确读取和显示,如DOC文档、图片文件、音视频等文件。 文件对象提供了三种读取文件内容的方法,分别是read、readline、readlines。其中read方法即可以读取文本文件也可以读取二级制文件,readline和readlines方法只能读取文本文件。下面分别予以说明。 使用read方法读取文件内容 read方法按字节读取文件内容,可以设定读取的字节数,read语法如下: content = fileobj.read(size=-1); read方法的size参数用于指定需要从文件读取的字节数,如果调用read方法时,没有给出size参数(默认值为-1),文件内容会被全部读取

使用readlines()读取文件时出现/n及其解决办法

為{幸葍}努か 提交于 2019-12-03 04:34:06
想要实现将文件中的数据全部读取并存入一个列表的功能,文件内容如下,打算使用readlines()进行操作。 初始代码: 1 f = open('page_id.txt', 'r', encoding='UTF-8') 2 lines = f.readlines() 在使用readlines()函数来读取文件的时候,得到的结果却是带 换行符\n 的: 那么只能手动将列表中的\n消除掉。这边总结两个方案: 方案一: f = open('page_id.txt', 'r', encoding='UTF-8') lines = f.readlines()for i in range(0,len(lines)-1): lines[i] = lines[i].strip('\n') print(lines) 使用for循环将列表中每个元素中的\n都删掉,这里用到了strip()函数。用法贴在后面。 方案二: f = open('page_id.txt', 'r', encoding='UTF-8') lines = f.readlines() l_length = len(lines) lines = ''.join(lines) #将列表类型转换为字符转类型 lines.strip('\n') #strip函数可以将字符串 lines = lines.split('\n') #将去掉

对文件的操作(2)

☆樱花仙子☆ 提交于 2019-12-03 00:05:31
对文件的操作(2) #_author:星#date:2019/10/30#(1)# num=0# f=open('ltx','r',encoding='utf8')# for i in f:#这是for 内部将f对象用作一个迭代器,用的时候一行一行去取# num+=1# if num==3:# i=''.join([i.strip(),'爱的真多'])# print(i.strip())# f.close()#(2)# print(f.tell())#0 ,tell方法,判断光标位置的方法# print(f.read(5))#偏偏秉烛夜 read对中文和英文都是一个# print(f.tell())#15 对汉字是两个,对英文是一个# print(f.read(1))#游## f.seek(0)#seek任意调整光标位置,进行任意位置的读写操作# print(f.read(5))#偏偏秉烛夜#(3)# f=open('ltx1','w',encoding='utf8')# f.write('来童星加油')# f.flush() 将缓存里面的数据直接呈现出来,用于进度条的实现#(4)# import sys,time# for i in range(30): #方法一 # sys.stdout.write("*") # sys.stdout.flush() #方法二 # print(

buuctf

允我心安 提交于 2019-12-02 18:35:44
大白 | png图片改高度 png图片改高度 [外链图片转存失败(img-PojN2D3v-1567086301372)(evernotecid://74A3E6DA-E009-4797-AA60-5DEED9FE4F7A/appyinxiangcom/23464203/ENResource/p2321)] 基础破解 | archpr爆破 你竟然赶我走 | 打开 010editor打开 最后面flag ningen | easy binwalk foremost 爆破 LSB | ss [外链图片转存失败(img-3ZEH1eki-1567086301372)(evernotecid://74A3E6DA-E009-4797-AA60-5DEED9FE4F7A/appyinxiangcom/23464203/ENResource/p2322)] [外链图片转存失败(img-73OkHr5k-1567086301373)(evernotecid://74A3E6DA-E009-4797-AA60-5DEED9FE4F7A/appyinxiangcom/23464203/ENResource/p2323)] save bin 二维码扫描 rar | archpr爆破 qr | 二维码扫描 乌镇峰会种图 | 记事本打开 wireshark | 导出http对象 导出http对象

== comparisons against lines from readlines() fail

血红的双手。 提交于 2019-12-02 10:12:29
I am currently working on a small anagram program that takes all possible permutations of a word and compare them with a dictionary. However, I am unable to get the results to print. The culprit appears to be the == operator, if I put ''.join(words[i]) == compare[j] nothing prints, however, if I input hi and run the program with ''.join(words[i]) == "hi" the entire dictionary prints, but if I invert it to "hi" == compare[j] nothing prints. Thanks in advance for any help! import itertools run = input("Please enter a word: ") dictionary = "dictionary.txt" #input("Please enter the name of the

python(open文件读取)

蹲街弑〆低调 提交于 2019-12-02 06:41:28
一.open文件读取 1.open('file','mode')打开一个文件 file  要打开的文件名,需加路径(除非是在当前目录) mode  文件打开的模式 需要手动关闭close 2.with open('file','mode')as... 不需要手动关闭文件 3.'r': 以只读模式打开(默认)(必须保证文件存在) 文件名中出现汉字时,需在括号内加 u 就不会出现报错IOError file1 = open("D:\新方硕.txt","r") print file1.read() file1.close() file1 = open(u"D:\新方硕.txt","r") print file1.read() file1.close() read(size) 读取所有   返回字符串 括号接读取 size 字节 #read()不传参数时默认读取所有 file1 = open(u"D:\新方硕.txt","r") print file1.read() file1.close() #read(3)括号内参数3代表字节数,一个汉字3个字节 file1 = open(u"D:\新方硕.txt","r") print file1.read(3) file1.close() readline()默认读取一行   返回字符串   括号内填了子节数,则按字节读取 #readline(