Question

in python I have a list of strings, some contain purely numbers while others are dates,...

in python I have a list of strings, some contain purely numbers while others are dates, names, etc. I am trying to convert the strings containing only numbers into floats. this is the code I am using but it alwyas returns nothing. Python just says restart and nothing goes through. how can I get my code to do what I want?

f = open('GOOG-HistoricalQuotest.txt','r+')
ff=f.readlines()
clean = []
lst = []
for i in range(2,len(ff)+1):
try:
[float(i) for i in ff]
print(ff[i])
except:
ValueError

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import re
with open('GOOG-HistoricalQuotest.txt?') as f:
    content = f.readlines()
content = [x.strip() for x in content]
clean = []
lst = []
for i in content:
    if not re.match("^\d+?\.\d+?$", i) is None:
        lst.append(float(i))
print(lst)

**Comment for any further queries.

Add a comment
Know the answer?
Add Answer to:
in python I have a list of strings, some contain purely numbers while others are dates,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

  • Greeting! Kindly help me to solve my finals in PYTHON, I don't have a knowledge in...

    Greeting! Kindly help me to solve my finals in PYTHON, I don't have a knowledge in PYTHON, new student. Please, please, I'm begging, Kindly answers all the questions. I'm hoping to grant my request. Thanks in advanced. 1.) What is the output of the following snippet? l1 = [1,2] for v in range(2): l1.insert(-1,l1[v]) print(l1)        a.) [1, 2, 2, 2]        b.) [1, 1, 1, 2]        c.) [1, 2, 1, 2]        d.) [1,...

  • please use python and provide run result, thank you! click on pic to make it bigger...

    please use python and provide run result, thank you! click on pic to make it bigger For this assignment you will have to investigate the use of the Python random library's random generator function, random.randrange(stop), randrange produces a random integer in the range of 0 to stop-1. You will need to import random at the top of your program. You can find this in the text or using the online resources given in the lectures A Slot Machine Simulation Understand...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT