0

I have string of tuples

LL =  [ ("text1",2,3,N/A), ("text2",N/A,5,6),("text3",N/A,5,N/A) ]

I need

LL =  [ ("text1",2,3,-1), ("text2",-1,5,6), ("text2",-1,5,-1) ]

So simply remove N/A replace with -1. Do I need to read all lines? Data download shows N/A, its not a variable.

LL is string

Using ("text2",N/A,5,6). Running python type on [0] is 'str'. Running python type on [1] , i get no output.

If I split the line above as a, b,c,d, Since I want number not a N/A, is there a way to say if not number replace with a number (-1). WHILE SPLITTING each line. Or, if error replace with -1.

Here is another link with the same problem, notice [1] has a N/A. http://download.finance.yahoo.com/d/quotes.csv?s=GS&f=sb6vt1&e=.csv

After struggling with data download with ill tempered CSV fields. How could use try/Except format.

try: 
   IF XXX or YYY or ZZZ or AAA == 'N/A',
   (dont process data...skip to except and pass)
except:
   pass 
13
  • 1
    What is N/A? Do you mean "N/A"? Commented Feb 28, 2011 at 18:24
  • 1
    Is N/A a string? What you have now is not valid Python syntax unless you are dividing a value N by A. Commented Feb 28, 2011 at 18:25
  • 1
    There's no N/A, unless you define two variables N and A as numbers (or something overloading __div__ in a weird way). Commented Feb 28, 2011 at 18:26
  • 1
    What is N/A? In the form you wrote it, name is not defined. Commented Feb 28, 2011 at 18:27
  • 5
    I herewith nominate this question for the confusion-of-idea tag. Commented Feb 28, 2011 at 18:29

5 Answers 5

1

The source you're downloading from is a csv file, not some weird python typle thing. You can handle CSVs more directly and easily by using the CSV module instead of you're hacked together solution.

import csv, urllib
rawStockInfo = csv.reader(urllib.urlopen('http://download.finance.yahoo.com/d/quotes.csv?s=GS+AAPL+MSFT+AMZN&f=sb6vt1&e=.csv'))
stocks = [] #list for our cleaned up version
for stock in rawStockInfo:
    print stock
    #this replaces the string "N/A" with the int -1
    stockClean = [-1 if x == 'N/A' else x for x in stock]
    stocks.append(stockClean)
print stocks

Note that you may want to change that list expression into another loop over the cells in stock... This would try to convert any strings to integers, and also replace 'N/A' with -1.

stockClean = []
for cell in stock:
    try:
        stockClean.append(int(cell)) #turn the string '4' into the integer 4
    except ValueError:
        if cell == 'N/A':
            stockClean.append(-1)
        else:
            stockClean.append(cell)
Sign up to request clarification or add additional context in comments.

8 Comments

using your thought on single dl worked, if the site has multi tickers, then string is output and blows up like the the rest.
I am uisng some like the ans from link and above URL. Any help? Your method doesnt work for the problem stated a above. stackoverflow.com/questions/3637553/python-url-download
Edited to reflect multiple stocks. Also added a section in case you want to do further cleaning up. Just replace the stockClean = [-1... with the second piece.
Now, i remember why I used .read vs. cvs.reader. Some fields have ",".
Shouldn't matter - they should be handled by the CSV reader none the less. I don't understand what you mean by "thanks, how would change to all variables sequentially?"
|
1

This list comprehension will loop through each item in LL and replace the string "N/A" with -1.

LL = [ ("text1",2,3,"N/A"), ("text2","N/A",5,6),("text3","N/A",5,"N/A") ]
LL = [tuple(x if x != "N/A" else -1 for x in i) for i in LL]
print LL
# [('text1', 2, 3, -1), ('text2', -1, 5, 6), ('text3', -1, 5, -1)]

If N/A is not a string then your code doesn't look like valid Python, edit your post and I will try to modify my answer accordingly.

Unfortunately, if you don't know where the N/A entries will be there is no way to replace them all without looping through all of your data.

edit: If LL is a string, then as others have said replace should do what you want, here is a complete example:

LL = '[ ("text1",2,3,N/A), ("text2",N/A,5,6),("text3",N/A,5,N/A) ]'
LL = LL.replace("N/A", "-1")
print LL
# [ ("text1",2,3,-1), ("text2",-1,5,6),("text3",-1,5,-1) ]

2 Comments

your code split every character, Some like [('"',), ('T',), ('E',), ('"',), (',',), ('N',), ............
You should edit your question to make it more clear that LL is a string, my method operates on LL as a list of tuples, which is what it looks like in your question.
1

Based on comments on all the other answers, I think I see what's going on here:

LL is a string, meant to be a representation of a list of tuples, so something like this:

LL = '[ ("text1",2,3,N/A), ("text2",N/A,5,6),("text3",N/A,5,N/A) ]'

will create the kind of string user428862 is getting from this other system. We can replace the "N/A" values and convert the string to a usable list using this code:

import ast
LL_replaced = LL.replace('N/A', '-1')
LL_as_list = ast.literal_eval(LL_replaced)

Now LL_as_list is a list of tuples, with -1 wherever the "N/A"s were in the original string. Is this what you were after?

4 Comments

Could you add an example of how to declare LL? Your LL = line, with the N/A's, will not run in a Python interpreter. My version (wrapping the whole thing in quotes) will, but it sounds like that's not what you meant.
copy from above and paste.--my example, not yours. then you'll see my problem Py think N/A is N divided by A, The data provider does not text "N/A".
So after you download a file like that, what do you use to read it in to a Python variable?
0

Maybe I don't understand well your question, but if your data is downloaded as you say, you can probably assign

LL = [ ("text1",2,3,N/A), ("text2",N/A,5,6),("text3",N/A,5,N/A) ]

to a string:

s = 'LL = [ ("text1",2,3,N/A), ("text2",N/A,5,6),("text3",N/A,5,N/A) ]'

Now, s.replace("N/A", "-1") should do the trick.

Comments

0

Try this code:

NEW = [tuple(x if isinstance(x, (basestring, int, long, float)) else -1 for x in i) for i in LL]

It replace all elements with unknown types.

1 Comment

I found that LL is a string, not list. output is same @andrew

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.