This could be easier one but this one is bugging me for a while. Help would be greatly appreciated.
Problem Definition: Make the list output looks good by removing the unicode value prefixed to the data.
Here is my code:
nfl = np.genfromtxt("drinks.csv", dtype="<U75", skip_header=1, delimiter=",")
print(nfl)
output is as follows:
[[u'Afghanistan' u'0' u'0' u'0' u'0.0']
[u'Albania' u'89' u'132' u'54' u'4.9']
[u'Algeria' u'25' u'0' u'14' u'0.7']
[u'Andorra' u'245' u'138' u'312' u'12.4']
[u'Angola' u'217' u'57' u'45' u'5.9']]
I also tried to print it like print(str(nfl)).No luck!How would I modify the output?
What I wanted to see is
[['Afghanistan' '0' '0' '0' '0.0']
['Albania' '89' '132' '54' '4.9']
['Algeria' '25' '0' '14' '0.7']
['Andorra' '245' '138' '312' '12.4']
['Angola' '217' '57' '45' '5.9']]
You can change the encoding to utf8 (the u stands for Unicode).
Code example:
item.encode('utf8')
Remember to import the library 'unicodedata'.
The commas are missing form your lists which won't help but I think it's just that you are seeing the list is its raw state. If you actually pull out the data you'll find the 'u' is gone.
data = [[u'Afghanistan', u'0', u'0', u'0', u'0.0'],
[u'Albania', u'89', u'132', u'54', u'4.9'],
[u'Algeria', u'25', u'0', u'14', u'0.7'],
[u'Andorra', u'245', u'138', u'312', u'12.4'],
[u'Angola', u'217', u'57', u'45', u'5.9']]
for country in data:
for item in country:
print item
I hope that's what you're asking.
@hpaulj comment helped me and I got the desired output.
nfl = np.genfromtxt("drinks.csv", dtype="S75", skip_header=1, delimiter=",")
print(nfl)
I have got my desired output:
[['Afghanistan' '0' '0' '0' '0.0']
['Albania' '89' '132' '54' '4.9']
['Algeria' '25' '0' '14' '0.7']
['Andorra' '245' '138' '312' '12.4']
['Angola' '217' '57' '45' '5.9']]
Thanks All!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.