How to read text file as list of floats? [duplicate] - python

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 1 year ago.
This seems like a simple question, but couldn't find it on the Stack community. I have a dataset like the one below in a text file. I would like to read it in as a list with each value as a float. Currently the output is very odd from the simple list needed (also below).
data.txt:
[1130.1271455966723, 1363.3947962724474, 784.433380329118, 847.2140341725295, 803.0276763894814,..]
Code attempted:
my_file = open(r"data.txt", "r")
content = my_file.read()
content_list = content.split(",")
my_file.close()
The output is odd. The values are string and list inside of list and added spaces:
Current result:
['[1130.1271455966723',
' 1363.3947962724474',
' 784.433380329118',
' 847.2140341725295',
' 803.0276763894814',
' 913.7751118925291',
' 1055.3775618432019',...]']
I also tried the approach here (How to convert string representation of list to a list?) with the following code but produced an error:
import ast
x = ast.literal_eval(result)
raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: ['[1130.1271455966723', '1363.3947962724474', ' 784.433380329118', ' 847.2140341725295', ' 803.0276763894814',...]']
Ideal result:
list = [1130.1271455966723, 1363.3947962724474, 784.433380329118, 847.2140341725295, 803.0276763894814]

Your data is valid JSON, so just use the corresponding module that will take care of all the parsing for you:
import json
with open("data.txt") as f:
data = json.load(f)
print(data)
Output:
[1130.1271455966723, 1363.3947962724474, 784.433380329118, 847.2140341725295, 803.0276763894814]

Related

I'm trying to see how many lines start with a specific character I.E "0" but I'm not getting any results. Where did I go wrong the "startswith()"? [duplicate]

This question already has answers here:
How does python startswith work?
(2 answers)
Closed 5 months ago.
I'm learning how to manipulate strings in python. I'm currently having an issue using the "startswith()" function. I'm trying to see how many lines start with a specific character I.E "0" but I'm not getting any results. Where did I go wrong? The text file only contains random generated numbers.
random = open("output-onlinefiletools.txt","r")
r = random.read()
#print(len(r))
#small = r[60:79]
#print(r[60:79])
#print(len(r[60:79]))
#print(small)
for line in random:
line = line.rstrip()
if line.startswith(1):
print(line)
You are searching for 1 as an int, and I wouldn't use random as it is not protected but is generally used as part of the random lib; the lines are treated as strings once read thus you need to use startswith on a string and not an int.
myFile = open("C:\Dev\Docs\output-onlinefiletools.txt","r")
r = myFile.read()
# return all lines that start with 0
for line in r.splitlines():
if line.startswith("0"):
print(line)
Output:
00000
01123
0000
023478
startwith takes the prefix as argument, in your case it will be line.startswith("0")

Format string output

With this python's code I may read all tickers in the tickers.txt file:
fh = open("tickers.txt")
tickers_list = fh.read()
print(tickers_list)
The output that I obtain is this:
A2A.MI, AMP.MI, ATL.MI, AZM.MI, BGN.MI, BMED.MI, BAMI.MI,
Neverthless, I'd like to obtain as ouput a ticker string exactly formatted in this manner:
["A2A.MI", "AMP.MI", "ATL.MI", "AZM.MI", ...]
Any idea?
Thanks in advance.
If you want the output to look in that format you want, you would need to do the following:
tickers_list= "A2A.MI, AMP.MI, ATL.MI, AZM.MI, BGN.MI, BMED.MI, BAMI.MI"
print("["+"".join(['"' + s + '",' for s in tickers_list.split(",")])[:-1]+"]")
With the output:
["A2A.MI"," AMP.MI"," ATL.MI"," AZM.MI"," BGN.MI"," BMED.MI"," BAMI.MI"]
Code explanation:
['"' + s + '",' for s in tickers_list.split(",")]
Creates a list of strings that contain each individual value, with the brackets as well as the comma.
"".join(...)[:-1]
Joins the list of strings into one string, removing the last character which is the extra comma
"["+..+"]"
adds the closing brackets
Another alternative is to simple use:
print(tickers_list.split(","))
However, the output will be slightly different as in:
['A2A.MI', ' AMP.MI', ' ATL.MI', ' AZM.MI', ' BGN.MI', ' BMED.MI', ' BAMI.MI']
Having ' instead of "
A solution for that however is this:
z = str(tickers_list.split(","))
z = z.replace("'",'"')
print(z)
Having the correct output, by replacing that character
you can to use Split function:
tickers_list = fh.read().split(',')

Single quotes around list elements that should be floats

I am asked to "return a list of tuples containing the subset name (as a string) and a list of floating point data values".
My code is:
def load_data(filename):
fileopen = open(filename)
result_open=[]
for line in fileopen:
answer = (line.strip().split(","))
result_open.append((answer[0],(answer[1:])))
return result_open
However, when I run the code, the following appears:
[('Slow Loris', [' 21.72', ' 29.3', ' 20.08', ' 29.98', ' 29.85', ' 26.22', ' 19......)]
Is there anyway to change the tuple to appear without the apostrophes? I want it to look like:
[('Slow Loris', [21.72, 29.3, 20.08, 29.98, 29.85, 6.22, 19......)]
line is a string, and line.strip().split(",") is a list of strings. You need to convert the string values into float or Decimal values. One way would be:
result_open.append((answer[0], [float(val) for val in answer[1:]]))
That will raise an exception on values that can't be converted to a float, so you should think about how you want to handle such input.

How to get data from json using python

json:
{"id":"1","name":"Smokey Mountain Ski Club","terrain_park":"Unknown","night_skiing":"Unknown","operating_status":"Unknown","latitude":52.977947,"longitude":-66.92094,"user":{"id":"7","username":"skier"},"tags":[{"id":"1","name":"Downhill"}],"ski_maps":[{"id":"902"}],"open_ski_maps":[],"created":"2008-04-13T00:11:59+00:00","regions":[{"id":"335","name":"Newfoundland and Labrador"}]}
I've done so that this data stores in a "data" variable..
I am trying to output all the data like: "key" : "value" list
for q in data:
print q + ': ' data[q]
This code outputs:
night_skiing: Unknown
name: Smokey Mountain Ski Club
Traceback (most recent call last):
TypeError: coercing to Unicode: need string or buffer, list found
I understand what's this error about but I don't know how to solve it..
So my question is how to put all the data from this array into variables?
When e.g. q == tags, your print line becomes:
print "tags" + ': ' + [{"id":"1","name":"Downhill"}]
Python is strongly typed, so you can't implicitly concatenate a (unicode) string with a list. You need to be explicit about what you want to happen, i.e. that the list (data[q]) should be converted to a string:
print q + ': ' str(data[q])
or, much better, use str.format rather than string concatenation (or the old-fashioned % formatting):
print "{0}: {1}".format(q, data[q])
Here you have a small example:
import json
try:
# parse JSON string from socket
p = json.loads(msg)
except (ValueError, KeyError, TypeError):
logging.debug("JSON format error: " + msg.strip() )
else:
# remote control is about controlling the model (thrust and attitude)
com = "%d,%d,%d,%d" % (p['r'], p['p'], p['t'], p['y'])
Problem is that not all your data values are strings, and you are treating them as strings.
Using a str format will convert the values to strings. Try this:
for q in data.iteritems():
print '%s: %s' % q
import json
data = '{"id":"1","name":"Smokey Mountain Ski Club","terrain_park":"Unknown","night_skiing":"Unknown","operating_status":"Unknown","latitude":52.977947,"longitude":-66.92094,"user":{"id":"7","username":"skier"},"tags":[{"id":"1","name":"Downhill"}],"ski_maps":[{"id":"902"}],"open_ski_maps":[],"created":"2008-04-13T00:11:59+00:00","regions":[{"id":"335","name":"Newfoundland and Labrador"}]}'
dataDict = json.loads(data)
for key in dataDict.keys():
print key + ': ' + str(dataDict[key])

Put function outputs to a list in Python [duplicate]

This question already has answers here:
How can I use `return` to get back multiple values from a loop? Can I put them in a list?
(2 answers)
How to concatenate (join) items in a list to a single string
(11 answers)
How can I print multiple things on the same line, one at a time?
(18 answers)
Closed 8 months ago.
The aim of the following program is to convert words in 4 characters from "This" to "T***", I have done the hard part getting that list and len working.
The problem is the program outputs the answer line by line, I wonder if there is anyway that I can store output back to a list and print it out as a whole sentence?
Thanks.
#Define function to translate imported list information
def translate(i):
if len(i) == 4: #Execute if the length of the text is 4
translate = i[0] + "***" #Return ***
return (translate)
else:
return (i) #Return original value
#User input sentense for translation
orgSent = input("Pleae enter a sentence:")
orgSent = orgSent.split (" ")
#Print lines
for i in orgSent:
print(translate(i))
On py 2.x you can add a , after print:
for i in orgSent:
print translate(i),
If you're on py 3.x, then try:
for i in orgSent:
print(translate(i),end=" ")
default value of end is a newline(\n), that's why each word gets printed on a new line.
Use a list comprehension and the join method:
translated = [translate(i) for i in orgSent]
print(' '.join(translated))
List comprehensions basically store the return values of functions in a list, exactly what you want. You could do something like this, for instance:
print([i**2 for i in range(5)])
# [0, 1, 4, 9, 16]
The map function could also be useful - it 'maps' a function to each element of an iterable. In Python 2, it returns a list. However in Python 3 (which I assume you're using) it returns a map object, which is also an iterable that you can pass into the join function.
translated = map(translate, orgSent)
The join method joins each element of the iterable inside the parentheses with the string before the .. For example:
lis = ['Hello', 'World!']
print(' '.join(lis))
# Hello World!
It's not limited to spaces, you could do something crazy like this:
print('foo'.join(lis))
# HellofooWorld!
sgeorge-mn:tmp sgeorge$ python s
Pleae enter a sentence:"my name is suku john george"
my n*** is s*** j*** george
You just need to print with ,. See last line of below pasted code part.
#Print lines
for i in orgSent:
print (translate(i)),
For your more understanding:
sgeorge-mn:~ sgeorge$ cat tmp.py
import sys
print "print without ending comma"
print "print without ending comma | ",
sys.stdout.write("print using sys.stdout.write ")
sgeorge-mn:~ sgeorge$ python tmp.py
print without ending comma
print without ending comma | print using sys.stdout.write sgeorge-mn:~ sgeorge$

Categories