readlines

大数据萌新的Python学习之路(三)

三世轮回 提交于 2020-02-13 21:34:02
笔记内容: 一、集合及其运算 在之列表中我们可以存储数据,并且对数据进行各种各样的操作。但是如果我们想要对数据进行去重时是十分麻烦的,需要使用循环,要建立新的列表,还要 进行对比,十分的麻烦,还消耗内存,所以我们在编程过程中就要使用集合。 Python中的集合和数学中的集合是一样的,也存在交并补的运算。集合的所有数据用花括号括起来,每个数据用逗号分隔。 我们可以来建立一个集合或者建立之后的列表通过set()来进行转换成集合。 1 set_1 = {1,5,15,25,48,78,44} 2 list_1 = [2,15,5,98,23,28] 3 set_2 = set(list_1) 4 print(set_1) 5 print(set_2) 输出结果: 我们建立了一个名为set_1的集合,名为list_1的列表,但是输出结果都是集合的形式,这是因为我们set_1是我们直接建立的集合,而set_2是我们通过set()用列表来转化而来的集合。集合中的数据也是和字典一样无序的,不能通过索引来存取数据。 set_1与set_2的两组数据之间是有重复的数据的5和15,那么如果我们想要把重复的数据取出来是,在数学中的做法是取交集,在Python中也是如此,像这样进行集合运算的操作叫做关系测试。 交集: 1 set_1 = {1,5,15,25,48,78,44} 2 set_2 = set

文件的读写操作

你离开我真会死。 提交于 2020-02-11 20:26:37
1 f = open('lyrics') #打开文件 2 first_line = f.readline()#一行一行读 3 print('---'.center(50,'-')) 4 data = f.read()# 读取剩下的所有内容,文件大时不要用 5 print(data) #打印文件 6 f.close() #关闭文件 打开文件有:读、写、添加。   r,只读模式(默认)。   w,只写模式。【不可读;不存在则创建;存在则删除内容;】   a,追加模式。【可读; 不存在则创建;存在则只追加内容;】 1 f = open('listen','r',encoding='utf-8') 2 f = open('listen','w',encoding='utf-8') 3 f = open('listen','a',encoding='utf-8') 这叫做句柄语句 若要把第九行去掉,可用下面方法: 1 for index,line in enumerate(f.readlines()) : #可以取出行号 2 if index == 9 : 3 print('abc'.center(50,"#")) 4 continue 5 print(line.strip()) View Code 用readlines()显示是一个列表 1 f = open('listen','r'

文件操作

半城伤御伤魂 提交于 2020-01-27 15:09:49
r+、w+、a+各做一次读写 追加写不管游标在哪个位置写的内容都在最后。 用readline读文件的所有内容: fp = open ( "f:\\a.txt" , "r" ) while 1 : content = fp . readline ( ) print ( content ) if content == "" : break fp . close ( ) readlines() with open ( "f:\\a.txt" , 'r' ) as f : for line in f : print ( line ) 小练习:写一个文件,内容如下: a26 b25 c24 d23 … z1 with open ( "f:\\a.txt" , 'w' ) as fp : for i in range ( 26 ) : fp . write ( chr ( ord ( "a" ) + i ) + str ( 26 - i ) + "\n" ) with open ( "f:\\a.txt" , 'r' ) as fp : print ( fp . read ( ) ) with open ( "f:\\a.txt" , 'w+' ) as fp : for i in range ( 26 ) : fp . write ( chr ( ord ( "a" ) + i ) +

remove line break from each element in python

一笑奈何 提交于 2020-01-21 09:44:17
问题 I open a text file, now each element in the text file is separated by a line break. when I use readlines() It keeps this line break in the list, this is after each element so as it looks like this [zebra\n, ant\n,] i was wondering whether there is a easy function or piece of coding that I could use to just strip all the elements of this line break! this is the code i have written so far filelist=file.readlines() file.close() return filelist 回答1: try: filelist = [line.rstrip('\n') for line in

Is there a maximum for the read in line of File.ReadLines()?

亡梦爱人 提交于 2020-01-06 03:07:16
问题 What happen if the file has very-long line(s)? Is there any limitation to how long a line is to iterate each of File.ReadLines() 回答1: I did not see any info regarding maximum length for File.ReadLines(). I would imagine the only limitation is that a string can't exceed 2GB, which is roughly 1 billion characters. That was listed on the MSDN page for string. 回答2: Since the documentation doesn't say anything about the limit of a line read from a file, the limit then becomes that of a regular

python - 文件操作

倖福魔咒の 提交于 2020-01-05 23:56:42
一、打开文件 1,import os context = "hello world! \n you are welecome !!!" 2,f = open('hello.txt','w',encoding= 'gbk') 3,f.write(context) 4,f.close() 二、文件读取 可以用readline()、readlines() 、 read()函数读取文件 1,按行读取方式readlin() f = open("hello.txt",'r') while True:   line = f.readline()   if line: #判断是否为空    print(line) else:    break f.close() 2,多行读取readlines() 使用readlines()读取文件,需要通过循环访问readlines()返回列表中的元素。 f = open("hello.txt",'r') lines = f.readlines() for line in lines:   print(line) f.close() 3,一次性读取方式read() 读取文件最简单的方法是使用read(),将文件中所以内容读出 f = open("hello.txt") context = f.read() print(context) f.close() 三

Python学习之文件操作

醉酒当歌 提交于 2020-01-05 23:37:31
Python 处理普通文件(flat file) Python处理普通文件时,把数据从一个文件中读入内存,然后从内存写入文件。 使用open()打开文件 读写一个文件之前要打开它,格式如下: fileobj = open(filename, mode) • fileobj 是open() 返回的文件对象; • filename 是该文件的字符串名; • mode 是指明文件类型和操作的字符串。 mode的第一个字母表明对文件的操作类型: • r 表示读模式。 • w 表示写模式。如果文件不存在则新创建,如果存在则重写新内容。 • x 表示在文件不存在的情况下新创建并写文件。 • a 表示如果文件存在,在文件末尾追加写内容。 mode的第二个字母表示要操作的文件的类型: • t(或者省略)代表文本类型; • b 代表二进制文件。 使用read()/readline()/readlines()读取文本文件 read() 使用read()函数能够一次性读取文件所有内容,如下: >>> f_read = open('text2.txt','rt') >>> content = f_read.read() >>> print(content) This is the first line this is the second line third line ... Nth line End.

Ignore last \n when using readlines with python

这一生的挚爱 提交于 2020-01-03 10:59:29
问题 I have a file I read from that looks like: 1 value1 2 value2 3 value3 The file may or may not have a trailing \n in the last line. The code I'm using works great, but if there is an trailing \n it fails. Whats the best way to catch this? My code for reference: r=open(sys.argv[1], 'r'); for line in r.readlines(): ref=line.split(); print ref[0], ref[1] Which would fail with a: Traceback (most recent call last): File "./test", line 14, in print ref[0], ref[1] IndexError: list index out of range

Ignore last \n when using readlines with python

本秂侑毒 提交于 2020-01-03 10:58:31
问题 I have a file I read from that looks like: 1 value1 2 value2 3 value3 The file may or may not have a trailing \n in the last line. The code I'm using works great, but if there is an trailing \n it fails. Whats the best way to catch this? My code for reference: r=open(sys.argv[1], 'r'); for line in r.readlines(): ref=line.split(); print ref[0], ref[1] Which would fail with a: Traceback (most recent call last): File "./test", line 14, in print ref[0], ref[1] IndexError: list index out of range

sys.stdin.readlines() hangs Python script

感情迁移 提交于 2019-12-29 06:09:50
问题 Everytime I'm executing my Python script, it appears to hang on this line: lines = sys.stdin.readlines() What should I do to fix/avoid this? EDIT Here's what I'm doing with lines : lines = sys.stdin.readlines() updates = [line.split() for line in lines] EDIT 2 I'm running this script from a git hook so is there anyway around the EOF? 回答1: This depends a lot on what you are trying to accomplish. You might be able do: for line in sys.stdin: #do something with line Of course, with this idiom as