0

I'm trying to remove a lot of stuff from a text file to rewrite it. The text file has several hundred items each consisting of 6 lines of. I got my code working to a point where puts all lines in an array, identifies the only 2 important in every item and deletes the whitespaces, but any further stripping gives me the following error:

'list' object has no attribute 'strip'

Here my code:

x = 0
y = 0
names = []
colors = []
array = []

with open("AA_Ivory.txt", "r") as ins:

    for line in ins:
        array.append(line)

def Function (currentElement, lineInSkinElement):
    name = ""
    color = ""
    string = array[currentElement]
    if lineInSkinElement == 1:
        string = [string.strip()]
#       string = [string.strip()]
#       name = [str.strip("\n")]
#       name = [str.strip(";")]
#       name = [str.strip(" ")]
#       name = [str.strip("=")]
        names.append(name)
        return name
#   if lineInSkinElement == 2:
#       color = [str.strip("\t")]
#       color = [str.strip("\n")]
#       color = [str.strip(";")]
#       color = [str.strip(" ")]
#       color = [str.strip("=")]
#       colors.append(color)
#       return color
    print "I got called %s times" % currentElement
    print lineInSkinElement
    print currentElement

for val in array:
    Function(x, y)
    x = x +1
    y = x % 6

#print names
#print colors

In the if statement for the names, deleting the first # will give me the error. I tried converting the list item to string, but then I get extra [] around the string.

The if statement for color can be ignored, I know it's faulty and trying to fix this is what got me to my current issue.

Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
Tharrry
  • 619
  • 1
  • 7
  • 23

3 Answers3

1

but then I get extra [] around the string

You can loop through this to get around the listed string. For example:

for lst, item in string:
    item = item.strip("\n")
    item = item.strip(";")
    item = item.strip(" ")
    item = item.strip("=")
    name.append(item)
    return name

This will get you to the string within the list and you can append the stripped string.

If this isn't what you were looking for, post some of the data you're working with to clarify.

snapcrack
  • 1,761
  • 3
  • 20
  • 40
  • This gets rid of only the \n elements. Getting rid of them was no problem. The problem is to continue the stripping after that. The line containing the name also contains unwanted and unneeded elements, i. e. "=" or ";" . However after it stripped the string for the first time it saves it or handles it as a list. – Tharrry Jun 18 '17 at 15:45
  • Right, I used `name.append(item)` as an example. You can strip more elements from the same string. What I'm saying is that if it saves it as a list, you need to iterate through the list in order to strip the string. – snapcrack Jun 18 '17 at 16:17
  • Oh, I see now. For some reason it was not showing me the middle part of your code. I tried the same thing except using the variable name 'str' instead of 'item' and it got me the error code stated in the post – Tharrry Jun 18 '17 at 22:43
  • Interesting. Can you post some of your data in your question? It might be easier to solve that way :) – snapcrack Jun 18 '17 at 22:50
  • 1
    @Harri - don't ever use variable names that are already used for built-in types/functions such as `str` or `list`, etc. as you cannot tell for sure what's the actual problem in such case. – zwer Jun 18 '17 at 23:07
  • Sure. One question though: for list, item in string - what exactly does it do? Iterate through the list, convert the current item to string and save it in the variable named 'item'? – Tharrry Jun 18 '17 at 23:08
  • @zwer - I'm very, very new to python, I didn't know it was already a function. – Tharrry Jun 18 '17 at 23:09
  • As for the data: It is, as mentioned, just a text file containing some hundred items with the following structure: class lincolngreen { displayName = "Lincoln Green"; texture = "#(argb,8,8,3)color(0.098,0.349,0.02,1,co)"; car = "all"; }; From this I need the value of color and displayName to the write a new text file with a different structure. – Tharrry Jun 18 '17 at 23:11
  • @Harri "for lst, item" iterates first over the list, then over the items within the list. If those items are strings, it allows you to access toe strings inside the list. A list is an object and a string is an object, so having two elements in the for loop allows you to specify which object you are trying to access. The way you have it will only try to modify the list itself, which doesn't have a strip property. Does that make sense? – snapcrack Jun 18 '17 at 23:17
  • However I don't understand how to apply it in my code. Do I need to replace 'lst' with the name of my list 'array'? Either way it tells me that string is not definded. – Tharrry Jun 18 '17 at 23:36
  • No, you can use any variable names you want for "for". They just keep track of he position of the objects – snapcrack Jun 19 '17 at 00:04
  • @Harri sorry I'm confused, did this solution work or not? – snapcrack Jun 19 '17 at 02:15
  • I got my code to work now. Answer is below yours. Thank you a lot for your help, patience, and explanations! – Tharrry Jun 19 '17 at 17:04
0

Alright, I found the solution. It was a rather dumb mistake of mine. The eerror occured due to the [] arroung the strip function making the outcome a list or list item. Removing them fixed it. Feeling relieved now, a bit stupid, but relieved.

Tharrry
  • 619
  • 1
  • 7
  • 23
0

You can also do that in one line using the following code.

item = item.strip("\n").strip("=").strip(";").strip()

The last strip will strip the white spaces.