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

标签 python arrays file split

我正在尝试从本地存储的 .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

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

事实是下面的代码可以工作并循环数组:

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

谁能帮帮我?

最佳答案

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

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

关于Python——从文件中读取行并拆分它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17741429/

相关文章:

python - 在 Django 中提供媒体服务

python - 从 Numpy 的 SVD 分解中获得负 S 值?

file - 按Xcode 4.3顺序播放多个音频文件

python - 如何在 Python 中读取一行 csv 数据?

java - 如何在其余服务中接收以八位字节流类型发送的图像?

python PIL - 背景显示不透明而不是透明

python - 为什么 numpy 和 scipy exp() 比 log() 更快?

java - 使用 Guava 更改大型二维数组的索引

c++ - 如何使用整数列表在 LLVM 中初始化整数数组?

python - numpy 数组大小的困惑