Python3 – 列表索引超出范围 – 从文件中提取数据

huangapple go评论95阅读模式
英文:

Python3 - list index out of range - extracting data from file

问题

我想从一个文件中提取数据,并使用 'for-loop' 更改一个条目的值。

f = open(r"C:\Users\Measurement\LOGGNSS.txt", "r")
x = 0
content = [[], []]
for line in f:
    actualline = line.strip()
    content.append(actualline.split(","))
    x += 1
f.close

print(x)
for z in range(x):
    print(z)
    print(content[z][1])

IndexError: list index out of range

在整个2D数组中使用实际值而不是变量 'z' 时可以正常工作。但我需要更改整个2D数组中的所有第一个条目。 为什么它不起作用?

英文:

I want to extract data from a file and change the value of an entry with a 'for-loop'.

f = open(r"C:\Users\Measurement\LOGGNSS.txt", "r")
x=0
content = [[],[]]
for line in f:
    actualline = line.strip()
    content.append(actualline.split(","))
    x+=1
f.close

print(x)
for z in range(x):
    print(z)
    print(content[z][1])

IndexError: list index out of range

Using a real value instead of the variable 'z' works fine. But I need to change all first entries in the whole 2D-Array.
Why it does not work?

答案1

得分: 0

你使用两个空数组来初始化你的内容,所以这两个都无法找到第一个索引([1]),只需用一个空数组来初始化它即可。

content = []

英文:

You initialize your content with two empty arrays, so both of these will fail to find the first index ([1]), just initialize it with an empty array

content = []

答案2

得分: 0

你的代码存在一些问题。
首先,使用 with 语句 来正确打开/关闭文件。
然后,你不需要使用像 x 这样的变量来跟踪行数,只需使用 enumerate() 即可!

以下是我如何重构你的代码,以使其更简洁和可读:

input_file = r"C:\Users\Measurement\LOGGNSS.txt"
content = []

with open(input_file, 'r') as f:
    for line in f:
        clean_line = line.strip().split(",")
        content.append(clean_line)

for z, data in enumerate(content):
    print(z, '\n', data)

注意,你可以在一次循环中读取文件并打印内容。

with open(input_file, 'r') as f:
    for z, line in enumerate(f):
        clean_line = line.strip().split(",")
        content.append(clean_line)
        print(z, '\n', clean_line)

最后,如果你处理的是一个简单的 CSV 文件,那么请使用标准库中的 csv 模块

import csv

with open(input_file, 'r') as f:
    content = csv.reader(f, delimiter=',')
英文:

Your code has several problems.
First of all, use the with statement to open/close files correctly.
Then, you don't need to use a variable like x to keep track of the number of lines, just use enumerate() instead!

Here is how I would refactor your code to make it slimmer and more readable.

input_file = r"C:\Users\Measurement\LOGGNSS.txt"
content = []

with open(input_file, 'r') as f:
    for line in f:
        clean_line = line.strip().split(",")
        content.append(clean_line)

for z, data in enumerate(content):
    print(z,'\n',data)

Note that you could print the content while reading the file in one single loop.

with open(input_file, 'r') as f:
    for z, line in enumerate(f):
        clean_line = line.strip().split(",")
        content.append(clean_line)
        print(z,'\n', clean_line)

Finally, if you are dealing with a plain and simple csv file, then use the csv module from the standard library.

import csv

with open(input_file, 'r') as f:
    content = csv.reader(f, delimiter=',')

huangapple
  • 本文由 发表于 2020年1月6日 18:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定