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...
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?
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...
Python: I want to make the following code to prompt the user if want to run...
Python: I want to make the following code to prompt the user if want to run the software again. I am new to python, so i do not know if it works like c++. If can explain that would be much appreciated base = float(input("Enter base of the triagle: ")) Triangle_Right = float(input("Enter right side of the triagle: ")) Triangle_Left = float(input("Enter left side of the triagle: ")) height = float(input("Enter the height of the triangle: ")) perimiter = base...
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to...
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to do a specific task. I have two text files, Positive.txt and Practice_forhw1.txt. I want to write a script that will check if any words in Practice_forhw1.txt match any of the words in Positive.txt then the word would get replaced with a "+1" in the Practice_forhw1.txt and then print out the Practice_forhw1.txt.  (i.e if the word "happy" is in both Positive.txt and Practice_forhw1.txt then I want...
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: read in 'marks.txt' . The file format is one number per line. Your program...
In Python: read in 'marks.txt' . The file format is one number per line. Your program will run through all these numbers, and make sure that they are valid input. you have to Create two lists: one for valid numbers and one for invalid numbers. For a number to be valid the number has to: Must be less than or equal to 100 Must be greater than or equal to 0 Must not have any letters inside of it (it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT