Python: How can read as float numbers a series of strings from a text file? - string

I'm trying to read from a text file line by line a series of strings like these:
11,52.15384615384615,52.84615384615385,14.0,45.15384615384615,39.76923076923077
10,27.09090909090909,54.81818181818182,64.36363636363636,65.54545454545455,21.90909090909091
(The first number is an integer used as index), and what I would like to get are float numbers such as
11, 52.15, 52.85, 14.00, 45.15, 39.77
10, 27.09, 54.82, 64.36, 65,54, 21.91
How can I convert these strings to a list of numbers?

Sounds like you are trying to get a list of floats together from a text file. You can use a dict to map the index you mention to the list of floats. Then just open the file, read line by line, use split(',') to split the string into a list of strings. Then grab the first integer as you index, use list slice to look the rest of the strings and convert/round them and add them to a new list which you can later assign to your index.
It's easier to read the code probably than it is to explain it.
my_float_dict = dict()
with open('my_float_strings.txt','r') as f:
for line in f:
string_list = line.split(',')
index = int(string_list[0])
line_float_list = []
for field in string_list[1:]:
line_float_list.append(round(float(field),2))
my_float_dict[index] = line_float_list
print my_float_dict

From your example I think this is what you are looking for.
Below the string s is converted to a float then rounded to 2 decimal points
>>>s='51.843256'
>>>round(float(s), 2)
51.84

Related

How to convert whole list of string into integer using python

Having list like l1=['abc.123','abc.456','abc.789','abc.153']
I want to sort this list based on numbers which are suffixes. But for that reason I have to convert this string list into integer. How can we do that.
Desired output is : l1= [abc.123,abc.153,abc.456,abc.789]
The safest way is to sort on the integer value of the part after the period, not of the last three characters. This allows for strings with fewer or more digits.
sorted(my_lst, key = lambda x: int(x.split(".")[-1]))
If we know that the last 3 characters are digits we could do:
sorted(my_lst, key = lambda x: int(x[-3:]))
['abc.123', 'abc.153', 'abc.456', 'abc.789']
or even noting that the are 3 numeric characters, you can go ahead and use them:
sorted(my_lst, key = lambda x: x[-3:])
['abc.123', 'abc.153', 'abc.456', 'abc.789']

Converting a scientific notation string to an integer

I have a question in Python 3.7. I am opening a text file (input.txt) and creating variables from various lines of the text file. For example my text file looks as follows:
Case1
P
1.00E+02
5.
I need to create variables so that:
title = "Case1"
area = "P"
distance = 100
factor = 5
So far this is what I have:
f = open('C:\\input.txt',"r")
title = f.readline().strip()
area = f.readline().strip()
distance = f.readline().strip()
factor = f.readline().strip().strip(".")
f.close
print(title)
print(area)
print(distance)
print(factor)
which results in:
Case1
P
1.00E+02
5
How do I get the distance variable to show up as 100 instead of 1.00E+02?
I found the link below thinking it would help, but wasn't able to solve my problem from there. This is a very small segment of my larger program that was simplified, but if it works here it will work for my needs. My program needs to open a generated text file and produce another text file, so the scientific notation number will change between 1.00E-06 and 1.00E+03. The generated text file needs to have these numbers as integers (i.e. between 0.000001 and 1000). Thanks for any help!
Converting number in scientific notation to int
The link you posted actually gives the answer, albeit in a roundabout way.
int() cannot parse strings that don't already represent integers.
But a scientific number is a float.
So, you need to cast it to float first to parse the string.
Try:
print(float(distance))
For numbers with more decimals (e.g your example of 1.00E-06), you can force float notation all the time. Try:
print(format(float(distance), 'f'))
If you only want a certain number of decimals, try:
print(format(float(distance), '.2f'))
(for example)

Set position according part of an objectname

I'm trying to script something in blender3D using python.
I've got a bunch of objects in my scene and want to translate them using a the numerical part of their objectname.
First of all i collect objects from the scene by matching a part of their name.
root_obj = [obj for obj in scene.objects if fnmatch.fnmatchcase(obj.name, "*_Root")]
This gives me a list with:[bpy.data.objects['01_Root'],bpy.data.objects['02_Root'],bpy.data.objects['03_Root'],bpy.data.objects['00_Root']]
My goal is to move these objects 15x their corresponding part of the name. So '00_Root' doesnt have to move, but '01_Root' has to move 15 blender units and '02_Root' 30 blender units.
How do i exctract the numberpart of the names and use them as translation values.
I'm a pretty newb with python so i would appreciate all the help i can get.
A string is a list of characters, each character can be accessed by index starting with 0, get the first character with name[0], the second with name[1]. As with any list you can use slicing to get a portion of the list. If the value is always the first two characters you can get the value with name[:2] you can them turn that into an integer with int() or a float with float(). Combined that becomes,
val = int(name[:2])
You then have a number you can calculate the new location with.
obj.location.x = val * 15
If the number of digits in the name might vary you can use split() to break the string on a specific separating character. This returns a list of items between the specified character, so if you want the first item to turn into an integer.
name = '02_item'
val = int(name.split('_')[0])
Using split also allows multiple values in a name.
name = '2_12_item'
val1 = int(name.split('_')[0])
val2 = int(name.split('_')[1])

Convert a string into an integer of its ascii values

I am trying to write a function that takes a string txt and returns an int of that string's character's ascii numbers. It also takes a second argument, n, that is an int that specified the number of digits that each character should translate to. The default value of n is 3. n is always > 3 and the string input is always non-empty.
Example outputs:
string_to_number('fff')
102102102
string_to_number('ABBA', n = 4)
65006600660065
My current strategy is to split txt into its characters by converting it into a list. Then, I convert the characters into their ord values and append this to a new list. I then try to combine the elements in this new list into a number (e.g. I would go from ['102', '102', '102'] to ['102102102']. Then I try to convert the first element of this list (aka the only element), into an integer. My current code looks like this:
def string_to_number(txt, n=3):
characters = list(txt)
ord_values = []
for character in characters:
ord_values.append(ord(character))
joined_ord_values = ''.join(ord_values)
final_number = int(joined_ord_values[0])
return final_number
The issue is that I get a Type Error. I can write code that successfully returns the integer of a single-character string, however when it comes to ones that contain more than one character, I can't because of this type error. Is there any way of fixing this. Thank you, and apologies if this is quite long.
Try this:
def string_to_number(text, n=3):
return int(''.join('{:0>{}}'.format(ord(c), n) for c in text))
print(string_to_number('fff'))
print(string_to_number('ABBA', n=4))
Output:
102102102
65006600660065
Edit: without list comprehension, as OP asked in the comment
def string_to_number(text, n=3):
l = []
for c in text:
l.append('{:0>{}}'.format(ord(c), n))
return int(''.join(l))
Useful link(s):
string formatting in python: contains pretty much everything you need to know about string formatting in python
The join method expects an array of strings, so you'll need to convert your ASCII codes into strings. This almost gets it done:
ord_values.append(str(ord(character)))
except that it doesn't respect your number-of-digits requirement.

Converting lists of digits stored as strings into integers Python 2.7

Among other things, my project requires the retrieval of distance information from file, converting the data into integers, then adding them to a 128 x 128 matrix.
I am at an impasse while reading the data from line.
I retrieve it with:
distances = []
with open(filename, 'r') as f:
for line in f:
if line[0].isdigit():
distances.extend(line.splitlines())`
This produces a list of strings.
while
int(distances) #does not work
int(distances[0]) # produces the correct integer when called through console
However, the spaces foobar the procedure later on.
An example of list:
['966']['966', '1513' 2410'] # the distance list increases with each additional city. The first item is actually the distance of the second city from the first. The second item is the distance of the third city from the first two.
int(distances[0]) #returns 966 in console. A happy integer for the matrix. However:
int(distances[1]) # returns:
Traceback (most recent call last):
File "", line 1, in
ValueError: invalid literal for int() with base 10: '1513 2410'
I have a slight preference for more pythonic solutions, like list comprehension and the like, but in reality- any and all help is greatly appreciated.
Thank you for your time.
All the information you get from a file is a string at first. You have to parse the information and convert it to different types and formats in your program.
int(distances) does not work because, as you have observed, distances is a list of strings. You cannot convert an entire list to an integer. (What would be the correct answer?)
int(distances[0]) works because you are converting only the first string to an integer, and the string represents an integer so the conversion works.
int(distances[1]) doesn't work because, for some reason, there is no comma between the 2nd and 3rd element of your list, so it is implicitly concatenated to the string 1513 2410. This cannot be converted to an integer because it has a space.
There are a few different solutions that might work for you, but here are a couple of obvious ones for your use case:
distance.extend([int(elem) for elem in line.split()])
This will only work if you are certain every element of the list returned by line.split() can undergo this conversion. You can also do the whole distance list later all at once:
distance = [int(d) for d in distance]
or
distance = map(int, distance)
You should try a few solutions out and implement the one you feel gives you the best combination of working correctly and readability.
My guess is you want to split on all whitespace, rather than newlines. If the file's not large, just read it all in:
distances = map(int, open('file').read().split())
If some of the values aren't numeric:
distances = (int(word) for word in open('file').read().split() if word.isdigit())
If the file is very large, use a generator to avoid reading it all at once:
import itertools
with open('file') as dists:
distances = itertools.chain.from_iterable((int(word) for word in line.split()) for line in dists)

Resources