简体   繁体   中英

Problems with “AttributeError: 'list' object has no attribute 'split'” in Python

I'm new to Python and I'm trying to figure out what would be the best way for me to parse some information from a text file.

This is how the given file looks like:

#
0.010000 125 _
0.130000 125 d
0.225000 125 o
0.260000 125 b
0.275000 125 a
0.335000 125 r
0.400000 125 v
0.455000 125 a
0.530000 125 m
0.580000 125 d

The # marks the beggining of the file.
I'd like to make three variables from each line. For the first line it would look something like this:

x = 0.010000  
y = 125  
z = "_" 

Because I'm working with tkinter, this is how my current code looks like:

    def cutFile(fileAudio, fileTime):
        path = fileAudio.get()
        fileAudioName = os.path.split(path)[1]
        pathTime = fileTime.get()
        fileTimeName = os.path.split(pathTime)[1]
        sound_file = AudioSegment.from_mp3(fileAudioName)
        timeFile = open(pathTime, "r")
        line = timeFile.readlines()
        newLine = line.split("\n")
        for i in range(1, len(newLine)):
            x, y, z = newLine.split(" ")
            print(z)

The problem seems to already start at the line

    newLine = line.split("\n")

because I'm getting the AttributeError: 'list' object has no attribute 'split' error.

If anyone could point me into the right direction, or suggest a better way of doing this, that would be really nice.

Try this:

lines = timeFile.readlines()
for x, l in enumerate(lines):
    if l.strip() == '#':
        break
else
    raise TypeError("invalid file content, missing '#' line")

for i in range(x + 1, len(lines), 1):
    l = lines[i]
    x, y, z = l.split()
    print(z)

What is going on here? After you open you read your file by calling timeFile.readlines() . This returns list of lines. So first we seek for your "starting" line containing only # . We strip whitespaces, as we usually don't want to deal with them and "# " and "#" looks almost the same and is easy to miss in hand edited file. After we found that line, we iterate from next till the end of file and for every line l we split it by calling split() and print z .

Use:

with open(pathTime) as infile:                #Open file for read
    next(infile)   #Skip #                   
    for line in infile:                       #Iterate Each line
        x, y, z = line.strip().split(" ")     #Strip leading and trailing space and split
        print(x, y, z)

Output:

0.010000 125 _
0.130000 125 d
0.225000 125 o
0.260000 125 b
0.275000 125 a
0.335000 125 r
0.400000 125 v
0.455000 125 a
0.530000 125 m
0.580000 125 d

In your code you do not need newLine = line.split("\\n") because readlines() already has split the content of the file by \\n

Ex:

timeFile = open(filename, "r")
lines = timeFile.readlines()

for line in lines[1:]:
    x, y, z = line.strip().split(" ")
    print(z)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM