【问题标题】:Reading particular fields from a text file (2d approach) and appending them to a list从文本文件中读取特定字段(二维方法)并将它们附加到列表中
【发布时间】:2021-06-02 23:44:02
【问题描述】:

我有一个文件 dates.txt 并试图将文件中的所有“名称”读入临时列表并将它们显示(打印)到屏幕上。在这种情况下,我希望输出为:

乔, 鱼, 米歇尔, 乔纳森

分叉的 repl.it - https://repl.it/@oiuwdeoiuas/Demo-of-2d-array-reading-from-text-file-1 到目前为止我的代码:

import csv 

    def filecontents(): 
     print("=========READ FIRST NAME===========")
     with open("dating.txt",newline="") as f:
        reader=list(csv.reader(f))
        temporarylist=reader #store copy of the file contents here
    
        for row in reader: #for every row in the file
          for i in row:
              names=[]
              names.append(temporarylist[i][2])
        print("The first name in the file is:",names)
    
    filecontents()

示例:dating.txt

JoeBbird,open123,Joe,Bloggs,M,jblogs@gmail.com,22/44/32,Christian,patience,7
FishSmith,open123,Fish,Bloggs,M,jblogs@gmail.com,22/44/32,Christian,laidback,0
MichelleFray,open123,Michelle,Fray,F,jblogs@gmail.com,22/44/32,Christian,leadership,0
JMartin,open123,Jonathan,Martin,M,jblogs@gmail.com,22/44/32,Christian,patience,0

在这里我注意到,假设采用二维数组方法,名称将采用以下格式:0、1、2、3(每行)和第 2 列。

因此,我尝试使用 for 循环遍历行并保持列(字段)标识符静态:

for i in row:
                  names=[]
                  names.append(temporarylist[i][2])

错误是:我正在努力理解/纠正。

TypeError: list indices must be integers or slices, not str

据我所知,列表索引 [0,1,2 等] 和 2 是整数。

(temporarylist[i][2])

谁能指出我的代码中的错误并解释我做错了什么。

【问题讨论】:

    标签: python list csv file


    【解决方案1】:

    csv阅读器是一个可迭代的行,每一行都是一个可迭代的字段,每个字段都是一个字符串。例如第一行应该是列表:

    ['JoeBbird', 'open123', ..., 'Christian', 'patience', '7']
    

    因此,当您尝试将一行迭代到 i 时,您会得到一个字符串。

    但如果我正确理解了你的问题,你不应该遍历行。

    只打印名字:

    def filecontents(): 
     print("=========READ FIRST NAME===========")
     with open("dating.txt",newline="") as f:
        reader=csv.reader(f)
        row = next(reader)
        print("The first name in the file is:",row[2])
    

    返回所有名称:

    def filecontents(): 
     print("=========READ FIRST NAME===========")
     with open("dating.txt",newline="") as f:
        reader = csv.reader(f)
        names = []   
        for row in reader: #for every row in the file
              names.append(row[2])
     return names
    

    您没有理由将完整文件存储在列表中,因为它只会浪费内存。但如果你有理由这样做:

    def filecontents(): 
     print("=========READ FIRST NAME===========")
     with open("dating.txt",newline="") as f:
        reader=csv.reader(f)
        temporarylist = list(reader)
        print("The first name in the file is:",temporarylist[0][2])
    

    【讨论】:

    • 我已经让它工作了,但是使用temporaryreader - 在你的答案中你可以包括:>(因为它是我现有代码的解决方案)。另外 - 没有像您所做的那样有一个临时列表有什么好处吗? for row in temporarylist: #for 文件中的每一行 names.append(row[2]) print("文件中的名字是:",names)
    • 非常感谢 - 在这种情况下,下一步是编辑列表 - 所以我假设(你能确认吗?)将文件存储在列表中是编辑文件内容的唯一方法
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2013-07-22
    • 2023-03-02
    • 1970-01-01
    • 2014-04-23
    相关资源
    最近更新 更多