Question

In: Computer Science

Code in python Hello I want a program to read only the price of each line...

Code in python

Hello I want a program to read only the price of each line in a txt file in python. The price is present right after the 2nd comma within the txt file. The output that I want is also printed at the bottom.

Inside the txt file:

2019-08-01 00:08:39,BTC/USD,10128.08

2019-08-01 00:08:21,BTC/USD,10117.54

2019-08-01 00:08:20,BTC/USD,10120.51

2019-08-01 00:08:19,BTC/USD,10118.13

2019-08-01 00:08:18,BTC/USD,10120.74

Python file output:

Price 1: 10128.08

Price 2: 10117.54

Price 3: 10120.51

Price 4: 10118.13

Price 5: 10120.74

Solutions

Expert Solution

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding).

Images of the Code:


File:- I have named file as text.txt. Please place the file where the python file is placed.

Note: If the below code is missing indentation please refer code Images

Typed Code:

#getting file name as input from the user
file_name = input("Enter file name: ")
#opening the file
file = open(file_name)
#reading file content
file_content = file.read()
#splitting lines of the file using '\n'(new line character)
lines = file_content.split('\n')
#closing file
file.close()
#opening file again to append prices
file = open(file_name, "a")
#for loop will iterate len(lines) times
for i in range(len(lines)):
#splitting lines using ',' comma
line = lines[i].split(',')
#if len(line) < 3
if len(line) < 3 :
#break
break
#storing line[2] to content
content = '\nPrice {}: {}'.format(i+1,line[2])
#writing the content to file
file.write(content)
#writing new line in file
file.write('\n')
#printing output added successfully to screen
print("Output added to the file '{}' Successfully.".format(file_name))
#closing file
file.close()
#code ended here

Output:

Prices printed at the bottom. Inside the txt file(text.txt) :-


If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!


Related Solutions

can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
I want to make a python program that encodes/decodes inputted messages. There are only three options...
I want to make a python program that encodes/decodes inputted messages. There are only three options in the initial menu: 1. encode, 2. decode, 3. quit, and if 1, 2, or 3 are not the input the program says that it is not valid, try again. If encode is chosen, the user inputs their message, the message should only be alphabetical and spaces. Then, the user can pick a number in the range [-27,27] with space being the 27th character...
In python explain why would we want to read a file line by line instead of...
In python explain why would we want to read a file line by line instead of all at once?
hello, I need a python program for a drone to follow keyboard commands - Create a...
hello, I need a python program for a drone to follow keyboard commands - Create a simple single obstacle environment and program the drone to navigate through it - Create a more advanced obstacle environment (3+ obstacles) and program the drone to navigate through it
Using Python, I am trying to integrate 'iloc' into the line of code below? This line...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line is replacing all the '1' values across my .csv file and is throwing off my data aggregation. How would I implement 'iloc' so that this line of code only changes the '1's integers in my 'RIC' column? patient_data_df= patient_data_df.replace(to_replace=[1], value ="Stroke") Thank you:)
Using Python code create a program only with beginners code. You are taking online reservations at...
Using Python code create a program only with beginners code. You are taking online reservations at the The inn Ask for your client’s name and save in a variable Ask how many nights your client will be staying and save in a variable Room rental is $145 per night Sales tax is 8.5% Habitation tax is $5 per night (not subject to sales tax) Print out: Client’s name Room rate per night Number of nights Room cost (room rate *...
code in python I want to make a function that adds all the multipliers together. The...
code in python I want to make a function that adds all the multipliers together. The code is posted below and please look at wanted output section to see what I want the code to output. The last line of the code is the only addition I need. Thanks. Code: initial=int(input("Initial value : "))       #taking inputs multiplier=float(input("Multiplier : ")) compound=int(input("No of compounds : ")) print("Your values are:") mult=initial                         #initalizing to find answer for i in range(0,compound):         #multiplying by multiplier    ...
In python using tkinter, I want to create a program. The program can have various classes...
In python using tkinter, I want to create a program. The program can have various classes and methods but I want it to have circles, triangles, and squares. Each shape movies in a certain way, like circles can move linearly, triangles can be affected by gravity, and squares can only go up and down. If the shapes get too close to one another then they disappear and a new shape appears somewhere else. I want this program to run continuously.
Hello I have a data set of stock price from previous year. I want to know...
Hello I have a data set of stock price from previous year. I want to know the probability of stock price to be more than a certain value after x days starting from where the dataset ends. Which way do i need to approach. I thought of logistic regression, if its correct what do i need to do to move forward. Thank you!
I already have the code of this program, I just want to know how to fix...
I already have the code of this program, I just want to know how to fix the code to Implement the 5th function (System.nanotime() and System.currentTimeMillis() methods) What Code does: Introduction Searching is a fundamental operation of computer applications and can be performed using either the inefficient linear search algorithm with a time complexity of O (n) or by using the more efficient binary search algorithm with a time complexity of O (log n). Task Requirements In this lab, you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT