Remove/Replace Error from Tuple in python
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...s开发者_如何学Gokip to except and pass)
except:
pass
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)
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) ]
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?
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.
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.
精彩评论