使用Python::Update with my answer::筛选或省略.txt文件中的多行类似数据:

使用Python::Update with my answer::筛选或省略.txt文件中的多行类似数据:,python,Python,我想做的是: 随附文件按字母顺序显示了美国的县议会名单。您需要对文件的每一行执行以下操作: 对于列为:Carter,KY的县城,将其更改为KY:Carter,并将其写入文件“file.txt”中,例如阿拉巴马州或俄亥俄州,忽略该行,不要将其写入输出文件 当我运行代码时,它会在阿拉巴马州停止运行,并带有一个builtin.indexeror:list索引超出范围,我需要过滤掉所有的州(拼写) 文件中的示例: Adams, WI Addison, VT Aiken, SC Aitkin, MN

我想做的是:

随附文件按字母顺序显示了美国的县议会名单。您需要对文件的每一行执行以下操作:

对于列为:Carter,KY的县城,将其更改为KY:Carter,并将其写入文件“file.txt”中,例如阿拉巴马州或俄亥俄州,忽略该行,不要将其写入输出文件

当我运行代码时,它会在阿拉巴马州停止运行,并带有一个builtin.indexeror:list索引超出范围,我需要过滤掉所有的州(拼写)

文件中的示例:

Adams, WI

Addison, VT

Aiken, SC

Aitkin, MN

ALABAMA

Alachua, FL
我的代码:

with open("c:\\Python\\MyFiles\\countyfile.txt", "r") as file:
    
    names = []
    state = []
    
    for line in file:
        splitLine = line.split(",")
        print(splitLine)
        names.append(splitLine[0])
        state.append(splitLine[1])
        
    print(names)Inde
    print(state)
错误消息:

builtins.IndexError: list index out of range
如果您能帮助我们忽略与各州之间的界线,我们将不胜感激

想发布我是如何完成整个任务的

感谢大家的帮助,我最终完成了整个程序的编码

enter code here

#open original file
file = open("c:\\Python\\MyFiles\\countyfile.txt", "r")

#creates or rewrites destination file
outfile = open("c:\\Python\\MyFiles\\file.txt", "w")

# variables
county = []
state = []


#for loop for text with ,
for line in file:
    if ',' in line:
        #Splits line and strips newline \n
        splitLine = line.rstrip('\n').split(",")
        #assigns county variable first line of the split
        county.append(splitLine[0])
        #assigns state variable second line of the split
        state.append(splitLine[1])
        #sets conditions to write to the outfile
        outputline = splitLine[1] + ": " + splitLine[0] + "," + '\n'
        #Print for testing purposes
        #print(splitLine[1] + splitLine[0] + ",")
        #writes output file
        outfile.write(outputline)


 else:
         continue


 #closes original and destination files
file.close()
outfile.close()

谢谢你的帮助

之所以会出现这种情况,是因为包含ALABAMA的行不包含逗号,以便根据逗号进行拆分。您可以更改逻辑,以便:

names = []
state = []

with open("c:\Python\MyFiles\us-counties.2.txt", "r") as file:
   for line in file:
       if ',' in line:
           splitLine = line.split(",")
           print(splitLine)
           names.append(splitLine[0])
           state.append(splitLine[1])
       else:
            continue
    
print(names)
print(state)

这是因为包含ALABAMA的行不包含逗号,因此无法基于逗号进行拆分。您可以更改逻辑,以便:

names = []
state = []

with open("c:\Python\MyFiles\us-counties.2.txt", "r") as file:
   for line in file:
       if ',' in line:
           splitLine = line.split(",")
           print(splitLine)
           names.append(splitLine[0])
           state.append(splitLine[1])
       else:
            continue
    
print(names)
print(state)

您可以复制粘贴状态列表,然后调用:

states = [state.capitalize() for state in states] # Where states is the list of states (eg. ['Alabama', 'Alaska'...])
在此之后,将其称为:

names = []
more_states = []
with open("c:\Python\MyFiles\us-counties.2.txt", "r") as file:
    for line in file:
        if line not in states:
            splitLine = line.split(',')
            names.append(splitLine[0])
            more_states.append(splitLine[1][1 : len(splitLine)]) # To get rid of the whitespace. I couldn't remember the string method for removing whitespace.

您可以复制粘贴状态列表,然后调用:

states = [state.capitalize() for state in states] # Where states is the list of states (eg. ['Alabama', 'Alaska'...])
在此之后,将其称为:

names = []
more_states = []
with open("c:\Python\MyFiles\us-counties.2.txt", "r") as file:
    for line in file:
        if line not in states:
            splitLine = line.split(',')
            names.append(splitLine[0])
            more_states.append(splitLine[1][1 : len(splitLine)]) # To get rid of the whitespace. I couldn't remember the string method for removing whitespace.

在将值分配给名称和状态之前,请使用
if len(splitLine)>1:
进行此测试。在将值分配给名称和状态之前,请使用
if len(splitLine)>1:
进行此测试。