my 'if' function won't work with strings? [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
It's a simple question:
lacount=0
ptcount=0
for line in list1:
print(str(line))
if 'LA'==str(line):
lacount+=1
if 'PT'==str(line):
print('pt works')
ptcount+=1
I'm trying to count how many 'PT' and 'LA' there are in a list but it seems like the if statements are not working, as my value still remains as a zero. Can someone help please?
The list I print out via the coding above comes out as:
PMID
TI
DP
AU
AU
AU
JT
LA
PT
PMID
TI
DP
AU
JT
LA
PT
PMID
TI
LID
DP
JT
AU
AU
LA
PT
PT = 0
LA = 0

Adding a strip() will remove any whitespaces that might've been in the string:
lacount=0
ptcount=0
for line in list1:
print(str(line))
if 'LA'==str(line).strip():
lacount+=1
if 'PT'==str(line).strip():
print('pt works')
ptcount+=1

I can't see your reference text that feeds into this function but try this:
lacount, ptcount = 0, 0
for line in list1:
print(str(line))
if 'LA' in str(line):
lacount+=1
if 'PT' in str(line):
print('pt works')
ptcount+=1
If you have multiple occurrences in a line:
lacount=0
ptcount=0
for line in list1:
laccount += str(line).count('LA')
ptcount += str(line).count('PT')

Related

Binary numbers to list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have written the following program in Python:
s = []
for e in random_key:
s = str(e)
print(s)
where the list random_key is
random_key = ['0011111011100101', '0000010111111011', '0011100110110100',
'1000010101010010', '0011001011001111', '1101101101110011',
'1100001111111011', '0000100000110100', '0101111010100101',
'1001100101100001']
The output of the program is
1111011010110011
1011000110011100
0011011001100010
0000011100100001
1111111010000100
0110110101100011
1011100011000101
1011101011100010
1101101101001010
1000011110110000
which is not correct. How can I fix the code?
If I am able to read your thoughts (not sure about that ..). Would you like them to 10 based numbers?
random_key = ['0011111011100101', '0000010111111011', '0011100110110100',
'1000010101010010', '0011001011001111', '1101101101110011',
'1100001111111011', '0000100000110100', '0101111010100101',
'1001100101100001']
numbers = [int(x, 2) for x in random_key]
print(numbers)
output
[16101, 1531, 14772, 34130, 13007, 56179, 50171, 2100, 24229, 39265]
Do you mean this?
s = list()
for e in random_key:
s.append(str(e))
print(s)
Returns:
['0011111011100101', '0000010111111011', '0011100110110100', '1000010101010010', '0011001011001111', '1101101101110011', '1100001111111011', '0000100000110100', '0101111010100101', '1001100101100001']

Multiple regex replacements in a loop do not work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm attempting to replace URL's and #username mentions of twitter data using Python's regular expression and a for loop.
d = df['text']
for i, e in enumerate(d):
d[i] = re.sub('((www.\.[\s]+)|(https?://[^\s]+))','URL', e)
d[i] = re.sub('#[^\s]+', 'AT_USER', e)
The problem is that the for loop only works for the second line of regex code ('AT_USER'). I want to replace the URL AND #username mentions. I was thinking of making two separate for loops for each but surely there's a more effective way?
So, the issue with your code as of now is here -
# vvv
d[i] = re.sub('#[^\s]+', 'AT_USER', e)
You should be passing d[i] instead of e. The fact that you pass e means you overwrite the result of the first replacement. Change it, and it should work.
You're using pandas. It's time to ditch the loop. First, initialise a dictionary of regex-replacement pairs -
p_dict = {r'((www.\.[\s]+)|(https?://[^\s]+))' : 'URL', r'#[^\s]+' : 'AT_USER'}
Now, pass this to df.replace with the regex switch -
df['text'] = df['text'].replace(p_dict, regex=True)
Here's a little example with some dummy data -
s
0 12.2
1 12.5
2 12.6
3 15.1
4 15.3
5 15.0
dtype: object
s[0]
Out[190]: '12.2' # a string
p_dict = {'\d' : '<DIGIT>', '\.' : '<DOT>'}
s.replace(p_dict, regex=True)
0 <DIGIT><DIGIT><DOT><DIGIT>
1 <DIGIT><DIGIT><DOT><DIGIT>
2 <DIGIT><DIGIT><DOT><DIGIT>
3 <DIGIT><DIGIT><DOT><DIGIT>
4 <DIGIT><DIGIT><DOT><DIGIT>
5 <DIGIT><DIGIT><DOT><DIGIT>
dtype: object

Issue when accessing hash by key [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am doing my first Python project and I have the following code:
response = urlopen(request)
flights = response.read()
text_file = open("Output.txt", "w")
text_file.write(flights)
text_file.close()
minPrice = LARGE_CONSTANT
for flight in flights["Quotes"]
if(flight["MinPrice"] < minPrice)
minPrice = flight["MinPrice"]
I am getting this error:
for flight in flights["Quotes"]
^
SyntaxError: invalid syntax
It seems like a very basic issue, but I'm unable to figure out what's wrong.
The hash flights has the following content:
{"Quotes":[{"QuoteId":1,"MinPrice":721.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":40074,"DepartureDate":"2016-05-15T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":40074,"DestinationId":60987,"DepartureDate":"2016-05-24T00:00:00"},"QuoteDateTime":"2016-04-16T13:58:00"},{"QuoteId":2,"MinPrice":490.0,"Direct":false,"OutboundLeg":{"CarrierIds":[835],"OriginId":50290,"DestinationId":42644,"DepartureDate":"2016-05-14T00:00:00"},"InboundLeg":{"CarrierIds":[1368],"OriginId":42563,"DestinationId":50290,"DepartureDate":"2016-05-23T00:00:00"},"QuoteDateTime":"2016-04-17T21:04:00"},{"QuoteId":3,"MinPrice":596.0,"Direct":false,"OutboundLeg":{"CarrierIds":[835],"OriginId":50290,"DestinationId":42644,"DepartureDate":"2016-05-28T00:00:00"},"InboundLeg":{"CarrierIds":[1710],"OriginId":42644,"DestinationId":50290,"DepartureDate":"2016-05-29T00:00:00"},"QuoteDateTime":"2016-04-07T22:10:00"},{"QuoteId":4,"MinPrice":574.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":60987,"DestinationId":42664,"DepartureDate":"2016-05-06T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":42664,"DestinationId":60987,"DepartureDate":"2016-05-08T00:00:00"},"QuoteDateTime":"2016-04-13T02:09:00"},{"QuoteId":5,"MinPrice":856.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1368],"OriginId":50290,"DestinationId":42664,"DepartureDate":"2016-05-05T00:00:00"},"InboundLeg":{"CarrierIds":[1368],"OriginId":42664,"DestinationId":50290,"DepartureDate":"2016-05-10T00:00:00"},"QuoteDateTime":"2016-04-15T23:11:00"},{"QuoteId":6,"MinPrice":741.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1033],"OriginId":60987,"DestinationId":43139,"DepartureDate":"2016-05-09T00:00:00"},"InboundLeg":{"CarrierIds":[1033],"OriginId":43139,"DestinationId":60987,"DepartureDate":"2016-05-16T00:00:00"},"QuoteDateTime":"2016-04-15T16:28:00"},{"QuoteId":7,"MinPrice":729.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1324],"OriginId":65633,"DestinationId":45676,"DepartureDate":"2016-05-07T00:00:00"},"InboundLeg":{"CarrierIds":[1324],"OriginId":45676,"DestinationId":60987,"DepartureDate":"2016-05-14T00:00:00"},"QuoteDateTime":"2016-04-07T05:58:00"},{"QuoteId":8,"MinPrice":848.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1033],"OriginId":60987,"DestinationId":47777,"DepartureDate":"2016-05-09T00:00:00"},"InboundLeg":{"CarrierIds":[1033],"OriginId":47777,"DestinationId":60987,"DepartureDate":"2016-05-16T00:00:00"},"QuoteDateTime":"2016-04-09T10:48:00"},{"QuoteId":9,"MinPrice":804.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1368],"OriginId":50290,"DestinationId":49369,"DepartureDate":"2016-05-27T00:00:00"},"InboundLeg":{"CarrierIds":[1368],"OriginId":49369,"DestinationId":50290,"DepartureDate":"2016-05-30T00:00:00"},"QuoteDateTime":"2016-04-18T09:39:00"},{"QuoteId":10,"MinPrice":576.0,"Direct":false,"OutboundLeg":{"CarrierIds":[838],"OriginId":50290,"DestinationId":49369,"DepartureDate":"2016-05-03T00:00:00"},"InboundLeg":{"CarrierIds":[838],"OriginId":49369,"DestinationId":60987,"DepartureDate":"2016-05-05T00:00:00"},"QuoteDateTime":"2016-04-09T15:12:00"},{"QuoteId":11,"MinPrice":726.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":60987,"DestinationId":49793,"DepartureDate":"2016-05-17T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":49793,"DestinationId":60987,"DepartureDate":"2016-05-24T00:00:00"},"QuoteDateTime":"2016-04-17T21:30:00"},{"QuoteId":12,"MinPrice":1172.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":50340,"DepartureDate":"2016-05-01T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":50340,"DestinationId":60987,"DepartureDate":"2016-05-04T00:00:00"},"QuoteDateTime":"2016-04-12T15:01:00"},{"QuoteId":13,"MinPrice":666.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1907],"OriginId":65633,"DestinationId":54353,"DepartureDate":"2016-05-10T00:00:00"},"InboundLeg":{"CarrierIds":[1907],"OriginId":54353,"DestinationId":65633,"DepartureDate":"2016-05-21T00:00:00"},"QuoteDateTime":"2016-04-04T01:14:00"},{"QuoteId":14,"MinPrice":802.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1368],"OriginId":50290,"DestinationId":54353,"DepartureDate":"2016-05-18T00:00:00"},"InboundLeg":{"CarrierIds":[1368],"OriginId":54353,"DestinationId":50290,"DepartureDate":"2016-05-25T00:00:00"},"QuoteDateTime":"2016-04-18T01:59:00"},{"QuoteId":15,"MinPrice":754.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1324],"OriginId":60987,"DestinationId":59078,"DepartureDate":"2016-05-15T00:00:00"},"InboundLeg":{"CarrierIds":[1324],"OriginId":59078,"DestinationId":60987,"DepartureDate":"2016-05-22T00:00:00"},"QuoteDateTime":"2016-04-15T01:55:00"},{"QuoteId":16,"MinPrice":1375.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":50290,"DestinationId":59117,"DepartureDate":"2016-05-06T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":59117,"DestinationId":50290,"DepartureDate":"2016-05-13T00:00:00"},"QuoteDateTime":"2016-04-06T09:28:00"},{"QuoteId":17,"MinPrice":893.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":50290,"DestinationId":60946,"DepartureDate":"2016-05-10T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":60946,"DestinationId":60987,"DepartureDate":"2016-05-17T00:00:00"},"QuoteDateTime":"2016-04-10T16:56:00"},{"QuoteId":18,"MinPrice":735.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1324],"OriginId":60987,"DestinationId":65393,"DepartureDate":"2016-05-16T00:00:00"},"InboundLeg":{"CarrierIds":[1324],"OriginId":65393,"DestinationId":60987,"DepartureDate":"2016-05-23T00:00:00"},"QuoteDateTime":"2016-04-16T15:37:00"},{"QuoteId":19,"MinPrice":520.0,"Direct":false,"OutboundLeg":{"CarrierIds":[858],"OriginId":60987,"DestinationId":65465,"DepartureDate":"2016-05-21T00:00:00"},"InboundLeg":{"CarrierIds":[858],"OriginId":65698,"DestinationId":60987,"DepartureDate":"2016-05-24T00:00:00"},"QuoteDateTime":"2016-04-09T18:44:00"},{"QuoteId":20,"MinPrice":538.0,"Direct":false,"OutboundLeg":{"CarrierIds":[858],"OriginId":60987,"DestinationId":65465,"DepartureDate":"2016-05-01T00:00:00"},"InboundLeg":{"CarrierIds":[858],"OriginId":65465,"DestinationId":60987,"DepartureDate":"2016-05-03T00:00:00"},"QuoteDateTime":"2016-04-17T17:29:00"},{"QuoteId":21,"MinPrice":602.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1081],"OriginId":60987,"DestinationId":65655,"DepartureDate":"2016-05-03T00:00:00"},"InboundLeg":{"CarrierIds":[1081],"OriginId":65655,"DestinationId":50290,"DepartureDate":"2016-05-12T00:00:00"},"QuoteDateTime":"2016-04-06T19:36:00"},{"QuoteId":22,"MinPrice":580.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1001],"OriginId":60987,"DestinationId":65655,"DepartureDate":"2016-05-06T00:00:00"},"InboundLeg":{"CarrierIds":[1001],"OriginId":65655,"DestinationId":60987,"DepartureDate":"2016-05-25T00:00:00"},"QuoteDateTime":"2016-04-12T06:36:00"},{"QuoteId":23,"MinPrice":494.0,"Direct":false,"OutboundLeg":{"CarrierIds":[835],"OriginId":50290,"DestinationId":65698,"DepartureDate":"2016-05-12T00:00:00"},"InboundLeg":{"CarrierIds":[835],"OriginId":65698,"DestinationId":65633,"DepartureDate":"2016-05-14T00:00:00"},"QuoteDateTime":"2016-04-17T00:26:00"},{"QuoteId":24,"MinPrice":666.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1859],"OriginId":60987,"DestinationId":65698,"DepartureDate":"2016-05-05T00:00:00"},"InboundLeg":{"CarrierIds":[838],"OriginId":65698,"DestinationId":60987,"DepartureDate":"2016-05-17T00:00:00"},"QuoteDateTime":"2016-04-07T12:21:00"},{"QuoteId":25,"MinPrice":733.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1033],"OriginId":60987,"DestinationId":66076,"DepartureDate":"2016-05-09T00:00:00"},"InboundLeg":{"CarrierIds":[1033],"OriginId":66076,"DestinationId":60987,"DepartureDate":"2016-05-16T00:00:00"},"QuoteDateTime":"2016-04-09T06:06:00"},{"QuoteId":26,"MinPrice":5673.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1385],"OriginId":60987,"DestinationId":66270,"DepartureDate":"2016-05-03T00:00:00"},"InboundLeg":{"CarrierIds":[1385],"OriginId":66270,"DestinationId":60987,"DepartureDate":"2016-05-25T00:00:00"},"QuoteDateTime":"2016-04-13T20:36:00"},{"QuoteId":27,"MinPrice":1379.0,"Direct":true,"OutboundLeg":{"CarrierIds":[2058],"OriginId":50290,"DestinationId":66270,"DepartureDate":"2016-05-06T00:00:00"},"InboundLeg":{"CarrierIds":[2058],"OriginId":66270,"DestinationId":50290,"DepartureDate":"2016-05-21T00:00:00"},"QuoteDateTime":"2016-04-13T04:57:00"},{"QuoteId":28,"MinPrice":505.0,"Direct":true,"OutboundLeg":{"CarrierIds":[105],"OriginId":60987,"DestinationId":67662,"DepartureDate":"2016-05-12T00:00:00"},"InboundLeg":{"CarrierIds":[105],"OriginId":67662,"DestinationId":60987,"DepartureDate":"2016-05-20T00:00:00"},"QuoteDateTime":"2016-04-13T10:12:00"},{"QuoteId":29,"MinPrice":551.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":60987,"DestinationId":67662,"DepartureDate":"2016-05-20T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":67662,"DestinationId":60987,"DepartureDate":"2016-05-22T00:00:00"},"QuoteDateTime":"2016-04-12T02:42:00"},{"QuoteId":30,"MinPrice":906.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1324],"OriginId":50290,"DestinationId":68229,"DepartureDate":"2016-05-17T00:00:00"},"InboundLeg":{"CarrierIds":[1324],"OriginId":68229,"DestinationId":50290,"DepartureDate":"2016-05-24T00:00:00"},"QuoteDateTime":"2016-04-17T15:23:00"},{"QuoteId":31,"MinPrice":804.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1368],"OriginId":50290,"DestinationId":70060,"DepartureDate":"2016-05-27T00:00:00"},"InboundLeg":{"CarrierIds":[1368],"OriginId":70060,"DestinationId":50290,"DepartureDate":"2016-05-31T00:00:00"},"QuoteDateTime":"2016-04-18T14:38:00"},{"QuoteId":32,"MinPrice":618.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1324],"OriginId":60987,"DestinationId":70060,"DepartureDate":"2016-05-19T00:00:00"},"InboundLeg":{"CarrierIds":[1324],"OriginId":70060,"DestinationId":60987,"DepartureDate":"2016-05-22T00:00:00"},"QuoteDateTime":"2016-04-09T17:33:00"},{"QuoteId":33,"MinPrice":823.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":70745,"DepartureDate":"2016-05-16T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":70745,"DestinationId":60987,"DepartureDate":"2016-05-23T00:00:00"},"QuoteDateTime":"2016-04-16T21:40:00"},{"QuoteId":34,"MinPrice":1129.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1324],"OriginId":60987,"DestinationId":71017,"DepartureDate":"2016-05-23T00:00:00"},"InboundLeg":{"CarrierIds":[1324],"OriginId":71017,"DestinationId":60987,"DepartureDate":"2016-05-27T00:00:00"},"QuoteDateTime":"2016-04-03T21:25:00"},{"QuoteId":35,"MinPrice":798.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":82165,"DepartureDate":"2016-05-10T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":82165,"DestinationId":60987,"DepartureDate":"2016-05-17T00:00:00"},"QuoteDateTime":"2016-04-10T03:23:00"},{"QuoteId":36,"MinPrice":726.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1523],"OriginId":60987,"DestinationId":82398,"DepartureDate":"2016-05-09T00:00:00"},"InboundLeg":{"CarrierIds":[1523],"OriginId":82398,"DestinationId":60987,"DepartureDate":"2016-05-14T00:00:00"},"QuoteDateTime":"2016-04-16T23:32:00"},{"QuoteId":37,"MinPrice":475.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1368],"OriginId":50290,"DestinationId":42563,"DepartureDate":"2016-05-18T00:00:00"},"InboundLeg":{"CarrierIds":[1368],"OriginId":42563,"DestinationId":50290,"DepartureDate":"2016-05-25T00:00:00"},"QuoteDateTime":"2016-04-18T02:26:00"},{"QuoteId":38,"MinPrice":795.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":60987,"DestinationId":63721,"DepartureDate":"2016-05-04T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":63721,"DestinationId":60987,"DepartureDate":"2016-05-09T00:00:00"},"QuoteDateTime":"2016-04-17T18:04:00"},{"QuoteId":39,"MinPrice":799.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":60987,"DestinationId":66217,"DepartureDate":"2016-05-16T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":66217,"DestinationId":60987,"DepartureDate":"2016-05-23T00:00:00"},"QuoteDateTime":"2016-04-10T19:24:00"},{"QuoteId":40,"MinPrice":819.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":82649,"DepartureDate":"2016-05-16T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":82649,"DestinationId":60987,"DepartureDate":"2016-05-23T00:00:00"},"QuoteDateTime":"2016-04-08T13:48:00"},{"QuoteId":41,"MinPrice":773.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":65633,"DestinationId":44620,"DepartureDate":"2016-05-09T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":44620,"DestinationId":65633,"DepartureDate":"2016-05-11T00:00:00"},"QuoteDateTime":"2016-04-08T22:21:00"},{"QuoteId":42,"MinPrice":995.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":47540,"DepartureDate":"2016-05-15T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":47540,"DestinationId":60987,"DepartureDate":"2016-05-24T00:00:00"},"QuoteDateTime":"2016-04-16T13:57:00"},{"QuoteId":43,"MinPrice":6835.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":42843,"DepartureDate":"2016-05-06T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":42843,"DestinationId":60987,"DepartureDate":"2016-05-21T00:00:00"},"QuoteDateTime":"2016-04-17T19:06:00"},{"QuoteId":44,"MinPrice":6826.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":54367,"DepartureDate":"2016-05-05T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":54367,"DestinationId":60987,"DepartureDate":"2016-05-11T00:00:00"},"QuoteDateTime":"2016-04-13T09:33:00"}],"Places":[{"PlaceId":40074,"IataCode":"ABZ","Name":"Aberdeen","Type":"Station","CityName":"Aberdeen","CityId":"ABER","CountryName":"United Kingdom"},{"PlaceId":42563,"IataCode":"BFS","Name":"Belfast International","Type":"Station","CityName":"Belfast","CityId":"BELF","CountryName":"United Kingdom"},{"PlaceId":42644,"IataCode":"BHD","Name":"Belfast City","Type":"Station","CityName":"Belfast","CityId":"BELF","CountryName":"United Kingdom"},{"PlaceId":42664,"IataCode":"BHX","Name":"Birmingham","Type":"Station","CityName":"Birmingham","CityId":"BIRM","CountryName":"United Kingdom"},{"PlaceId":42843,"IataCode":"BLK","Name":"Blackpool","Type":"Station","CityName":"Blackpool","CityId":"BLAC","CountryName":"United Kingdom"},{"PlaceId":43139,"IataCode":"BRS","Name":"Bristol","Type":"Station","CityName":"Bristol","CityId":"BRIS","CountryName":"United Kingdom"},{"PlaceId":44620,"IataCode":"CAL","Name":"Campbeltown","Type":"Station","CityName":"Campbeltown","CityId":"CAMP","CountryName":"United Kingdom"},{"PlaceId":45676,"IataCode":"CWL","Name":"Cardiff","Type":"Station","CityName":"Cardiff","CityId":"CARD","CountryName":"United Kingdom"},{"PlaceId":47540,"IataCode":"DND","Name":"Dundee","Type":"Station","CityName":"Dundee","CityId":"DUND","CountryName":"United Kingdom"},{"PlaceId":47777,"IataCode":"DSA","Name":"Doncaster Sheffield","Type":"Station","CityName":"Doncaster","CityId":"DONC","CountryName":"United Kingdom"},{"PlaceId":49369,"IataCode":"EDI","Name":"Edinburgh","Type":"Station","CityName":"Edinburgh","CityId":"EDIN","CountryName":"United Kingdom"},{"PlaceId":49793,"IataCode":"EMA","Name":"East Midlands","Type":"Station","CityName":"Nottingham","CityId":"NOTT","CountryName":"United Kingdom"},{"PlaceId":50290,"IataCode":"EWR","Name":"New York Newark","Type":"Station","CityName":"New York","CityId":"NYCA","CountryName":"United States"},{"PlaceId":50340,"IataCode":"EXT","Name":"Exeter","Type":"Station","CityName":"Exeter","CityId":"EXET","CountryName":"United Kingdom"},{"PlaceId":54353,"IataCode":"GLA","Name":"Glasgow International","Type":"Station","CityName":"Glasgow","CityId":"GLAS","CountryName":"United Kingdom"},{"PlaceId":54367,"IataCode":"GLO","Name":"Gloucestershire","Type":"Station","CityName":"Gloucester","CityId":"GLOA","CountryName":"United Kingdom"},{"PlaceId":57113,"IataCode":"HUY","Name":"Humberside","Type":"Station","CityName":"Humberside","CityId":"HUMB","CountryName":"United Kingdom"},{"PlaceId":59078,"IataCode":"INV","Name":"Inverness","Type":"Station","CityName":"Inverness","CityId":"INVE","CountryName":"United Kingdom"},{"PlaceId":59117,"IataCode":"IOM","Name":"Ronaldsway","Type":"Station","CityName":"Castletown","CityId":"CAST","CountryName":"United Kingdom"},{"PlaceId":60946,"IataCode":"JER","Name":"Jersey","Type":"Station","CityName":"Jersey","CityId":"JERS","CountryName":"United Kingdom"},{"PlaceId":60987,"IataCode":"JFK","Name":"New York John F. Kennedy","Type":"Station","CityName":"New York","CityId":"NYCA","CountryName":"United States"},{"PlaceId":63721,"IataCode":"KOI","Name":"Orkney Kirkwall","Type":"Station","CityName":"Orkney","CityId":"ORKN","CountryName":"United Kingdom"},{"PlaceId":65393,"IataCode":"LBA","Name":"Leeds Bradford","Type":"Station","CityName":"Leeds","CityId":"LEED","CountryName":"United Kingdom"},{"PlaceId":65465,"IataCode":"LCY","Name":"London City","Type":"Station","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":65633,"IataCode":"LGA","Name":"New York La Guardia","Type":"Station","CityName":"New York","CityId":"NYCA","CountryName":"United States"},{"PlaceId":65655,"IataCode":"LGW","Name":"London Gatwick","Type":"Station","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":65698,"IataCode":"LHR","Name":"London Heathrow","Type":"Station","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":66076,"IataCode":"LPL","Name":"Liverpool","Type":"Station","CityName":"Liverpool","CityId":"LIVE","CountryName":"United Kingdom"},{"PlaceId":66217,"IataCode":"LSI","Name":"Sumburgh Shetlands","Type":"Station","CityName":"Sumburgh","CityId":"SUMB","CountryName":"United Kingdom"},{"PlaceId":66270,"IataCode":"LTN","Name":"London Luton","Type":"Station","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":67662,"IataCode":"MAN","Name":"Manchester","Type":"Station","CityName":"Manchester","CityId":"MANC","CountryName":"United Kingdom"},{"PlaceId":68229,"IataCode":"MME","Name":"Durham Tees Valley","Type":"Station","CityName":"Durham","CityId":"MMEA","CountryName":"United Kingdom"},{"PlaceId":70060,"IataCode":"NCL","Name":"Newcastle","Type":"Station","CityName":"Newcastle","CityId":"NEWC","CountryName":"United Kingdom"},{"PlaceId":70745,"IataCode":"NQY","Name":"Newquay","Type":"Station","CityName":"Newquay","CityId":"NEWQ","CountryName":"United Kingdom"},{"PlaceId":71017,"IataCode":"NWI","Name":"Norwich","Type":"Station","CityName":"Norwich","CityId":"NORW","CountryName":"United Kingdom"},{"PlaceId":82165,"IataCode":"SOU","Name":"Southampton","Type":"Station","CityName":"Southampton","CityId":"SOUT","CountryName":"United Kingdom"},{"PlaceId":82398,"IataCode":"STN","Name":"London Stansted","Type":"Station","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":82649,"IataCode":"SYY","Name":"Stornoway","Type":"Station","CityName":"Stornoway","CityId":"STOR","CountryName":"United Kingdom"},{"PlaceId":91075,"IataCode":"WIC","Name":"Wick","Type":"Station","CityName":"Wick","CityId":"WICK","CountryName":"United Kingdom"},{"PlaceId":3413153,"IataCode":"NYC","Name":"New York","Type":"City","CityName":"New York","CityId":"NYCA"}],"Carriers":[{"CarrierId":105,"Name":"Thomas Cook Airlines"},{"CarrierId":835,"Name":"Air Canada"},{"CarrierId":838,"Name":"Air France"},{"CarrierId":858,"Name":"Alitalia"},{"CarrierId":881,"Name":"British Airways"},{"CarrierId":1001,"Name":"Norwegian"},{"CarrierId":1033,"Name":"Aer Lingus"},{"CarrierId":1081,"Name":"Icelandair"},{"CarrierId":1324,"Name":"KLM"},{"CarrierId":1368,"Name":"Lufthansa"},{"CarrierId":1385,"Name":"EL AL Israel Airlines"},{"CarrierId":1523,"Name":"Austrian Airlines"},{"CarrierId":1710,"Name":"Brussels Airlines"},{"CarrierId":1859,"Name":"Virgin Atlantic"},{"CarrierId":1907,"Name":"WestJet"},{"CarrierId":2058,"Name":"La Compagnie"}],"Currencies":[{"Code":"USD","Symbol":"$","ThousandsSeparator":",","DecimalSeparator":".","SymbolOnLeft":true,"SpaceBetweenAmountAndSymbol":false,"RoundingCoefficient":0,"DecimalDigits":2}]}
Why can I not access the hash like this flights["Quotes"]?
Lacking a : at the end.
response = urlopen(request)
flights = response.read()
text_file = open("Output.txt", "w")
text_file.write(flights)
text_file.close()
minPrice = LARGE_CONSTANT
for flight in flights["Quotes"]:
if(flight["MinPrice"] < minPrice):
minPrice = flight["MinPrice"]
Kinda simple, sorry i can't be of more use.
But every python block (for, if, while, with) requires a : at the end of the initiating row followed by a indentation on the code that belongs to that block of code, in this case it's for ...: and well yea your if ...:
Also fixed your indentation (it should be 4 spaces or one tab per block of code)

convert csv columns to dictionary in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a csv file with the following data.
Column-1 Column-2 Column-3
bob sweet 4
alice uber 4.5
bob uber 4
alice sweet 4.5
razi fav 2.5
razi uber 3.5
bob fav 4
I want to convert it to a dictionary as shown,
A={'bob':{'sweet':'4', 'uber':'4', 'fav':'4'},
'alice':{'uber':'4.5', 'sweet':'4.5'},
'razi':{'fav':'2.5', 'uber':'3.5'}}
in python
For that i am willing to do like this..convert the csv to list like this and then get my output. I am unable to do so, coz keys are repeated as shown.
A={'bob':['sweet','4'],
'alice':['uber','4.5'],
'bob':['uber','4'],
'alice':['sweet','4.5'],
'razi':['fav','2.5'],
'razi':['uber','3.5'],
'bob':['fav','4']}
Can any one suggest a way to solve problem?
Assuming you don't have any space in your datas, and all your actual data rows have exactly 3 fields:
import logging
logging.basicConfig(level=logging.INFO) # <- in a real application,
# should be set application-wide
# from a config file
logger = logging.getLogger("CSV import")
result = {}
nlines = 0
ok = 0
warnings = 0
with open("my_file.csv") as f:
f.readline() # Skip header. Assuming only one line of heading
for row in (line.split() for line in f):
nlines += 1
try:
k1,k2, val = row
result.setdefault(k1,{})[k2] = val
ok += 1
except ValueError:
logger.warning("Format mismatch: %s", row)
warnings += 1
# what to do next?
logger.info("%d lines read. %d imported. %d warnings",nlines,ok,warnings)
from pprint import pprint
pprint(result)
Given your sample data file, this produces:
INFO:CSV import:7 lines read. 7 imported. 0 warnings
{'alice': {'sweet': '4.5', 'uber': '4.5'},
'bob': {'fav': '4', 'sweet': '4', 'uber': '4'},
'razi': {'fav': '2.5', 'uber': '3.5'}}
The trick here is to use setdefault to access to outer dictionary. It will either return the value if the key was already present -- or a new dictionary if this is the first time we encounter that key. After that, this is simply a matter of adding the value to the inner dictionary as usual.

how to assign random float number in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I used this code to generate xyz coordinates.
from random import *
uniqcoord = [7.63, 28.05, 66.36] # my file contains 100 such list of points
for i in range(10):
i = i + 1
x,y,z = uniqcoord
x1,y1,z1 = (uniform(x[0]-3.5,x[0]+3.5), uniform(y[1]-3.5,y[1]+3.5), uniform(z[2]-3.5,z[2]+3.5))
print i, '\t', x1,y1,z1
When i run this program its showing error.
when i run this program with hole numbers it work.
how to resolve????
Your x, y, z are float, not list. So your can't have x[0] and such. You will get a no attribute or typeerror for that.
You're incorrectly using x,y,z in the x1,y1,z1 line. See what I've done below;
from random import *
uniqcoord = [7.63, 28.05, 66.36] # my file contains 100 such list of points
for i in range(10):
i=i+1
x,y,z = uniqcoord
x1,y1,z1 = (uniform(uniqcoord[0]-3.5,uniqcoord[0]+3.5), uniform(uniqcoord[1]-3.5,uniqcoord[1]+3.5), uniform(uniqcoord[2]-3.5,uniqcoord[2]+3.5))
print i, '\t', x1,y1,z1
x[0] does not exist, but uniqcoord[0] = x because uniqcoord = [x,y,z], and so on.
Result:
1 5.86941266341 29.4004245806 67.1323961576
2 6.38143060206 29.7045813689 69.4867869245
3 5.55280335095 29.9472835241 63.7388152633
4 10.5607637875 26.6269381673 69.5256503601
5 7.29826364813 28.5740308696 65.2122276564
6 8.24134391937 30.880058802 69.8445734597
7 10.246919304 27.9240839326 64.9480054046
8 8.26957559527 28.5700768795 63.996117793
9 5.88677020227 30.0621250245 63.7431176092
10 8.98100830174 27.3378753286 63.1329446911
I think this is what you are looking for.

Categories