Python 读取指定行数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 读取指定行数相关的知识,希望对你有一定的参考价值。

b=1
c=2
F = ('n'.join(open('C:\\Users\\Administrator\\Desktop\\ID.txt','r').readlines()[str(b):str(c)]))
运行结果报错
NameError: name 'b' is not defined
怎样才能将变量代入,不会出错
运行结果报这个:TypeError: slice indices must be integers or None or have an __index__ method

F = ('n'.join(open('C:\\\\Users\\\\Administrator\\\\Desktop\\\\ID.txt','r', encoding='gbk').readlines()[b:c]))

追问

请问encoding='gbk'这个是什么意思?

追答

使用gbk编码打开你的txt文件,如果你的txt文件编码是utf-8的,那么这里就需要修改为utf-8

追问

F = ('n'.join(open('C:\\Users\\Administrator\\Desktop\\ID.txt','r', encoding='utf-8').readlines()[b:c]))
TypeError: 'encoding' is an invalid keyword argument for this function
还是报错了

参考技术A 不用str(b),直接[b:c]追问

直接写b和c 也报错了
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe0 in position 0: unexpected end of data

追答

试试open('C:\\Users\\Administrator\\Desktop\\ID.txt','r').readlines().decode('gbk')

追问

我的变量怎么带进去?

追答

open('C:\\Users\\Administrator\\Desktop\\ID.txt','r').readlines().decode('gbk')[b:c]

追问

额 还是有错AttributeError: 'list' object has no attribute 'decode'有没有联系方式私信一下吧

(转)Java按指定行数读取文件

package test  
import java.io.File;  
import java.io.FileReader;  
import java.io.IOException;  
import java.io.LineNumberReader;  
public class ReadSelectedLine{  
    // 读取文件指定行。  
    static void readAppointedLineNumber(File sourceFile, int lineNumber)  
            throws IOException {  
        FileReader in = new FileReader(sourceFile);  
        LineNumberReader reader = new LineNumberReader(in);  
        String s = "";  
        if (lineNumber <= 0 || lineNumber > getTotalLines(sourceFile)) {  
            System.out.println("不在文件的行数范围(1至总行数)之内。");  
            System.exit(0);  
        }  
        int lines = 0;  
        while (s != null) {  
            lines++;  
            s = reader.readLine();  
            if((lines - lineNumber) == 0) {  
             System.out.println(s);  
             System.exit(0);  
            }  
        }  
        reader.close();  
        in.close();  
    }  
    // 文件内容的总行数。  
    static int getTotalLines(File file) throws IOException {  
        FileReader in = new FileReader(file);  
        LineNumberReader reader = new LineNumberReader(in);  
        String s = reader.readLine();  
        int lines = 0;  
        while (s != null) {  
            lines++;  
            s = reader.readLine();  
        }  
        reader.close();  
        in.close();  
        return lines;  
    }  
      
    /** 
     * 读取文件指定行。 
     */  
    public static void main(String[] args) throws IOException {  
        // 指定读取的行号  
        int lineNumber = 2;  
        // 读取文件  
        File sourceFile = new File("D:/java/test.txt");  
        // 读取指定的行  
        readAppointedLineNumber(sourceFile, lineNumber);  
        // 获取文件的内容的总行数  
        System.out.println(getTotalLines(sourceFile));  
    }  
}  

  

以上是关于Python 读取指定行数的主要内容,如果未能解决你的问题,请参考以下文章

(转)Java按指定行数读取文件

Qt中怎样读取文件指定的行?

python 如何读取 excel 指定单元格内容

彩色折线散点图python怎么指定数据

Python和MATLAB读取excel指定行列数据的方法

java要怎么修改csv中指定行列位置的值