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?
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.
Python Explain Code #Python program class TreeNode:
Python Explain Code   #Python program class TreeNode:    def __init__(self, key):        self.key = key        self.left = None        self.right = Nonedef findMaxDifference(root, diff=float('-inf')):    if root is None:        return float('inf'), diff    leftVal, diff = findMaxDifference(root.left, diff)    rightVal, diff = findMaxDifference(root.right, diff)    currentDiff = root.key - min(leftVal, rightVal)    diff = max(diff, currentDiff)     return min(min(leftVal, rightVal), root.key), diff root = TreeNode(6)root.left = TreeNode(3)root.right = TreeNode(8)root.right.left = TreeNode(2)root.right.right = TreeNode(4)root.right.left.left = TreeNode(1)root.right.left.right = TreeNode(7)print(findMaxDifference(root)[1])
Python I want to name my hero and my alien in my code how do I...
Python I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name import random class Hero:     def __init__(self,ammo,health):         self.ammo=ammo         self.health=health     def blast(self):         print("The Hero blasts an Alien!")         if self.ammo>0:             self.ammo-=1             return True         else:             print("Oh no! Hero is out of ammo.")             return False     def...
WANT CODE IN PYTHON PROGRAM: THE QUESTION IS BASED ON Mcdonald.csv Plot graphically which food categories...
WANT CODE IN PYTHON PROGRAM: THE QUESTION IS BASED ON Mcdonald.csv Plot graphically which food categories have the highest and lowest varieties. Which all variables have an outlier? Which variables have the Highest correlation. Plot them and find out the value ? Which category contributes to the maximum % of Cholesterol in a diet (% daily value)? Which item contributes maximum to the Sodium intake? Which 4 food items contains the most amount of Saturated Fat?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT