Python——从文件中读取行并将其拆分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17741429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:03:19  来源:igfitidea点击:

Python -- Reading lines from file and split it

pythonarraysfilesplit

提问by Marc Ortiz

I'm trying to split the lines from a .txt file stored locally. What I do to make a for loop but I want to start the loop on an specific index on the array. For example:

我正在尝试从本地存储的 .txt 文件中拆分行。我做了什么来创建 for 循环,但我想在数组的特定索引上开始循环。例如:

  file_content = open('files/filefinal1_test.txt')
  counter = 0
  total_lines = 0
  global l
  index = 0 if l == "" else file_content.index(l)

  for line in file_content[index:]: 
    l = line
    array_line =line.split('", "')
    array_line[0] = array_line[0].replace('"', '')
    array_line[1] = array_line[1].replace('"', '')
    array_line[2] = array_line[2].replace('"', '')
    array_line[3] = array_line[3].replace('"', '')
    if (send_req(papi, array_line) == 1): 
        counter = counter + 1 
    total_lines = total_lines + 1

This gives me errors at : file_content[index:] It's there any way to start the loop at specific line from the file_content?

这给了我以下错误:file_content[index:] 有什么方法可以从 file_content 的特定行开始循环?

The fact it's that the code below works and loops the array :

事实上,下面的代码有效并循环数组:

for line in file_content: 
        l = line
        array_line =line.split('", "')
        array_line[0] = array_line[0].replace('"', '')
        array_line[1] = array_line[1].replace('"', '')
        array_line[2] = array_line[2].replace('"', '')
        array_line[3] = array_line[3].replace('"', '')
        if (send_req(papi, array_line) == 1): 
            counter = counter + 1 
        total_lines = total_lines + 1

Could anyone help me please?

有人可以帮我吗?

采纳答案by Marc Ortiz

I've fount the answer! I didn't call the method .readlines()

我已经找到答案了!我没有调用方法 .readlines()

file_content = file_content.readlines()
for lines in file_content[0:]:
     #stuff

回答by rkrzr

You can use lines = open(filepath, 'rb').readlines()to get a list of strings, where each string is a line in your file. You can then slice the list at any index you want to only get the lines you are interested in like this: wanted_lines = lines[index:]This will get you all the lines from index to the end of the file.

您可以使用lines = open(filepath, 'rb').readlines()获取字符串列表,其中每个字符串都是文件中的一行。然后,您可以在任何索引处对列表进行切片,只获取您感兴趣的行,如下所示:wanted_lines = lines[index:]这将获取从索引到文件末尾的所有行。

回答by Steve Allison

You can use islicefrom the itertoolsmodule to create a slice over a file (or any iterable):

您可以使用islicefromitertools模块在文件(或任何可迭代的)上创建切片:

import itertools
with open('file') as f:
    for line in itertools.islice(f, 3, 5): # 3 and 5 are the start and stop points
        print line

see http://docs.python.org/2/library/itertools.html#itertools.islicefor more

有关更多信息,请参见http://docs.python.org/2/library/itertools.html#itertools.islice