Reputation: 31
can anybody tell me what's wrong here. I am reading a text file containing a list of dictionaries. [code][1] 'am not sure why the second curly braces is added...I only get this error in a loop. Answers to similar errors seem to address using input() or raw_input. 'am reading directly from a text file like
with open('mybundle.txt', 'r') as R:
list = []
my_data = R.read()
my_data = my_data.replace('[','')
my_data = my_data.replace(']','')
my_data.rstrip('\r\n')
my_data = my_data.split('},')
import ast
for a in my_data:
rec = a+'}'
list.append(rec)
m = ast.literal_eval(list[100])
#Now this works..
print(m)
print(m['open'])
{'volume': 0, 'quoteVolume': 0, 'high': 260.00000522, 'low': 260.00000522, 'date': 1425801600, 'close': 260.00000522, 'weightedAverage': 260.00000522, 'open': 260.00000522}
260.00000522
try:
df_=[ast.literal_eval(x) for x in list]
df = pd.DataFrame(df_, index=['date'], columns=
["high","low","open","close","volume","quoteVolume","weightedAverage"])
except EOFError:
#my_data = pd.DataFrame(list(my_data))
print(df.head())
File "<unknown>", line 1 {"date":1503403200,"high":3959,"low":3838.4845461,"open":3881.999999,"close":3887.75413166,"volume":6580841.4708805,"quoteVolume":1683.41702938,"weightedAverage":3909.21640688}}
SyntaxError: unexpected EOF while parsing
Upvotes: 0
Views: 374
Reputation: 60143
Does this help you see the issue?
text = '{a},{b},{c}'
for part in text.split('},'):
print('Part: {}'.format(part))
print('Part with added curly brace: {}'.format(part + '}'))
# Output:
# Part: {a
# Part with added curly brace: {a}
# Part: {b
# Part with added curly brace: {b}
# Part: {c}
# Part with added curly brace: {c}}
You can fix this by not adding a curly brace to the last element of the split list, or you can do some sort of more sensible parsing. (If you can control how this file is created in the first place, consider using a serialization format like JSON instead.)
Upvotes: 1