Python ValueError: could not convert string to float - python

line = 'f 1// 2// 3// 4//'
vertices = []
line = line.split(" ")
toks = line[1:]
for vertex in toks:
l = vertex.split("/")
#print(l)
l = np.array[float(x) for x in l]).astype(_DT)
position = (l[0])
vertices.append(position)
print(vertices)
this is the output ['1', '2', '3', '3'] which is correct! but I am getting a "ValueError could not covert string to float: " at this line
l = np.array[float(x) for x in l]).astype(_DT)
but there are no leading or trailing whitespaces , not sure how to go about this error!!
when doing print(l) i get ['1', ''] ['2', ''] ['3', ''] ['4', '']
I tried using .strip() on line but that didn't do anything. Also tried replace(" ", "") didn't do anything either. I can't find where the actual problem is. How can I identify where there is white spaces? HOW do i remove the '' ?

You can skip the empty strings in your array comprehension with if. You can use the fact that empty strings evaluate as false, and any nonempty string will evaluate as true:
[float(x) for x in l if x]

Related

Why is replace not getting rid of my white space?

Im trying to use replace to get rid of white space but it is not working. What am i doing wrong?
import re
list = ('255 +1', '282 +5', '255 + 3', '5 - 2',)
for i in list:
# seperating the numbers in to a list
nums = re.split(r'[+,-]\s*', i)
#getting rid of white space in list
for num in nums:
num.replace(' ', '')
print(nums)
this is the output. in the first part of the lists it is not getting rid of it.
['255 ', '1']
['282 ', '5']
['255 ', '3']
['5 ', '2']
Strings are immutable objects in python, meaning their values cannot be changed. If you were to use the replace method you would have to set that to a new variable.
For example, if you wanted to use replace you would need to initialize a new String Variable and set it equal to num.replace(' ', ''), or alternatively use the strip() method with no parameters to remove trailing and leading white spaces.
If you wanted to reflect a new string without whitespaces...
myString = 'thisString ' #Notice the trailing whitespace
newString = myString.strip() #Removes whitespace and stores in newString
print(newString) #This would output the string with no white space
(1) we can use regex in the first step to just extract all numbers. this results in s list of 8 elements: ['255', '1', '282', '5', '255', '3', '5', '2']
(2) we create an index by using range() with step 2
(3) we use the index from previous step to create chunks (length 2) of pairs by using a slice and append them to a list
import re
regex = r"(\d+)"
test_str = ("'255 +1', '282 +5', '255 + 3', '5 - 2'\n")
matches = re.findall(regex, test_str) # (1)
res = []
for ind in range(0, len(matches), 2): # (2)
res.append(matches[ind:ind + 2]) # (3)
print(res)
output is [['255', '1'], ['282', '5'], ['255', '3'], ['5', '2']]

Reading a text file line by line and converting it into a list using only read()?

So I have a txt file that contains the following numbers:
10
5
6
2
3
4
1
9
34
22
5
There is only one number per row. I want to put all the numbers in a list and use only the read() function. readline() or readlines() is not allowed.
This is what I've tried doing (note that I have to use a function like this):
def get_list1(text):
result=[]
for row in text:
result.append(row)
return result
with open("file.txt") as f:
n = f.read()
l=get_list1(n)
print(l)
This is the output:
['1', '0', '\n', '5', '\n', '6', '\n', '2', '\n', '3', '\n', '4', '\n', '1',
'\n', '9', '\n', '3', '4', '\n', '2', '2', '\n', '5']
As you can see it includes the \n and splits the numbers into its digits.
I want an output of
['10','5','6','2','3','4','1','9','34','22','5']
You can use split():
def get_list1(text):
result=[]
for row in text:
result.append(row)
return result
with open("test.txt") as f:
n = f.read().split("\n")
l=get_list1(n)
print(l)
Or just use splitlines()
def get_list1(text):
result=[]
for row in text:
result.append(row)
return result
with open("test.txt") as f:
n = f.read().splitlines()
l=get_list1(n)
print(l)
The short way to do this is
def get_list1(text):
return text.split("\n")
with open("file.txt") as f:
get_list1(f.read())
or you can replace the function by something like
# takes care of empty lines as well and create actual integers in a list
l = list(map(int, (w.strip() for w in f.read().split() if w.strip())))
Yours is wrong because:
def get_list1(text):
# text is a big text with \n in it
result=[]
# iterates the big blob of text as single characters
for row in text:
result.append(row)
return result
If you can not use any split() you can parse your file once character at time:
def get_list1(text):
"""This function takes a text with included newlines \n, and processes it
characterwise. Each character is added into a list of lists where the last
of it is always a list. A character is added to that last inner list
if it is not a \n.
If a \n is encountered, the last inner list is converted to a string
(if not empty - to account for newlines in the input) and a new empty
list is added for the characters of the next line. At the end the case
of last character == \n is handled. A list of line texts is returned."""
result=[[]]
for character in text:
if character == "\n":
t = ''.join(result[-1]).strip()
if len(t) > 0:
# change list of characters into string
result[-1] = int(t)
result.append([])
else:
result[-1].clear()
continue
result[-1].append(character)
try:
t = ''.join(result[-1]).strip()
if len(t) > 0:
# change list of characters into string
result[-1] = int(t)
else:
result = result[:-1]
except:
result = result[:-1]
return result
print(get_list1("1\n11\n111\n111\n11111\n11\n"))
Output:
[1, 11, 111, 111, 11111, 11]

Separate each item of a list in an specific way

I have an input, which is a tuple of strings, encoded in a1z26 cipher: numbers from 1 to 26 represent alphabet letters, hyphens represent same word letters and spaces represent an space between words.
For example:
8-9 20-8-5-18-5 should translate to 'hi there'
Let's say that the last example is a tuple in a var called string
string = ('8-9','20-8-5-18-5')
The first thing I find logical is convert the tuple into a list using
string = list(string)
so now
string = ['8-9','20-8-5-18-5']
The problem now is that when I iterate over the list to compare it with a dictionary which has the translated values, double digit numbers are treated as one, so instead of, for example, translating '20' it translate '2' and then '0', resulting in the string saying 'hi bheahe' (2 =b, 1 = a and 8 = h)
so I need a way to convert the list above to the following
list
['8','-','9',' ','20','-','8','-','5','-','18','-','5',]
I've already tried various codes using
list(),
join() and
split()
But it ends up giving me the same problem.
To sum up, I need to make any given list (converted from the input tuple) into a list of characters that takes into account double digit numbers, spaces and hyphens altogether
This is what I've got so far. (The last I wrote) The input is further up in the code (string)
a1z26 = {'1':'A', '2':'B', '3':'C', '4':'D', '5':'E', '6':'F', '7':'G', '8':'H', '9':'I', '10':'J', '11':'K', '12':'L', '13':'M', '14':'N', '15':'O', '16':'P', '17':'Q', '18':'R', '19':'S', '20':'T', '21':'U', '22':'V', '23':'W', '24':'X', '25':'Y', '26':'Z', '-':'', ' ' : ' ', ', ' : ' '}
translation = ""
code = list(string)
numbersarray1 = code
numbersarray2 = ', '.join(numbersarray1)
for char in numbersarray2:
if char in a1z26:
translation += a1z26[char]
There's no need to convert the tuple to a list. Tuples are iterable too.
I don't think the list you name is what you actually want. You probably want a 2d iterable (not necessarily a list, as you'll see below we can do this in one pass without generating an intermediary list), where each item corresponds to a word and is a list of the character numbers:
[[8, 9], [20, 8, 5, 18, 5]]
From this, you can convert each number to a letter, join the letters together to form the words, then join the words with spaces.
To do this, you need to pass a parameter to split, to tell it how to split your input string. You can achieve all of this with a one liner:
plaintext = ' '.join(''.join(num_to_letter[int(num)] for num in word.split('-'))
for word in ciphertext.split(' '))
This does exactly the splitting procedure as described above, and then for each number looks into the dict num_to_letter to do the conversion.
Note that you don't even need this dict. You can use the fact that A-Z in unicode is contiguous so to convert 1-26 to A-Z you can do chr(ord('A') + num - 1).
You don't really need hypens, am I right?
I suggest you the following approach:
a = '- -'.join(string).split('-')
Now a is ['8', '9', ' ', '20', '8', '5', '18', '5']
You can then convert each number to the proper character using your dictionary
b = ''.join([a1z26[i] for i in a])
Now b is equal to HI THERE
I think, it's better to apply regular expressions there.
Example:
import re
...
src = ('8-9', '20-8-5-18-5')
res = [match for tmp in src for match in re.findall(r"([0-9]+|[^0-9]+)", tmp + " ")][:-1]
print(res)
Result:
['8', '-', '9', ' ', '20', '-', '8', '-', '5', '-', '18', '-', '5']
using regex here is solution
import re
string = '8-9 20-8-5-18-5'
exp=re.compile(r'[0-9]+|[^0-9]+')
data= exp.findall(string)
print(data)
output
['8', '-', '9', ' ', '20', '-', '8', '-', '5', '-', '18', '-', '5']
if you want to get hi there from the input string , here is a method (i am assuming all character are in uppercase):
import re
string = '8-9 20-8-5-18-5'
exp=re.compile(r'[0-9]+|[^0-9]+')
data= exp.findall(string)
new_str =''
for i in range(len(data)):
if data[i].isdigit():
new_str+=chr(int(data[i])+64)
else:
new_str+=data[i]
result = new_str.replace('-','')
output:
HI THERE
You could also try this itertools solution:
from itertools import chain
from itertools import zip_longest
def separate_list(lst, delim, sep=" "):
result = []
for x in lst:
chars = x.split(delim) # 1
pairs = zip_longest(chars, [delim] * (len(chars) - 1), fillvalue=sep) # 2, 3
result.extend(list(chain.from_iterable(pairs))) # 4
return result[:-1] # 5
print(separate_list(["8-9", "20-8-5-18-5"], delim="-"))
Output:
['8', '-', '9', ' ', '20', '-', '8', '-', '5', '-', '18', '-', '5']
Explanation of above code:
Split each string by delimiter '-'.
Create interspersing delimiters.
Create pairs of characters and separators with itertools.zip_longest.
Extend flattened pairs to result list with itertools.chain.from_iterable.
Remove trailing ' ' from result list added.
You could also create your own intersperse generator function and apply it twice:
from itertools import chain
def intersperse(iterable, delim):
it = iter(iterable)
yield next(it)
for x in it:
yield delim
yield x
def separate_list(lst, delim, sep=" "):
return list(
chain.from_iterable(
intersperse(
(intersperse(x.split(delim), delim=delim) for x in lst), delim=[sep]
)
)
)
print(separate_list(["8-9", "20-8-5-18-5"], delim="-"))
# ['8', '-', '9', ' ', '20', '-', '8', '-', '5', '-', '18', '-', '5']

Remove apostrophe from arraylist

I have some list of array, separated with ';'
O;4;State[1', '25', '3];CPUA.DB1610.274,X5;RW
V;5;LostClClamp;CPUA.DB1610.276,X3;RW
O;4;State[1', '26', '1];CPUA.DB1610.276,X5;RW
for example: result[0][2:3] == State[1', '25', '3]
And I want to remove apostrophe character:
for n in range(len(result)):
if "'" in result[n][2:3]:
result[n][2:3].replace("'", "")
But this code not work like I wanted.
Assuming result is the list containing the strings, ie. result="O;4;State[1', '25', '3];CPUA.DB1610.274,X5;RW".split(';')
for i, x in enumerate(result):
result[i] = result.replace("'","")
replace() method does not modify the string, it just returns a copy, thus you need to type:
result[n][2:3] = result[n][2:3].replace("'", "")

How do I remove hyphens from a nested list?

In the nested list:
x = [['0', '-', '3', '2'], ['-', '0', '-', '1', '3']]
how do I remove the hyphens?
x = x.replace("-", "")
gives me AttributeError: 'list' object has no attribute 'replace', and
print x.remove("-")
gives me ValueError: list.remove(x): x not in list.
x is a list of lists. replace() will substitute a pattern string for another within a string. What you want is to remove an item from a list. remove() will remove the first occurrence of an item. A simple approach:
for l in x:
while ("-" in l):
l.remove("-")
For more advanced solutions, see the following: Remove all occurrences of a value from a Python list

Categories

Resources