Python: getting info from file? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Users are prompted for the location of a file that contains multiple sets of some information: the product number, the price of the product, the day, the month, the year.
eg.
12345678,200,1,1,2014
23456789,150,1,1,2014
12345678,180,1,2,2014
I need to get the total number of prices (ie 200,150,180). How do I do so?

Open your file, iterate over its lines with a for loop, split each line by commas and print second field.
This should work:
filepath = raw_input('Input file path:\n')
with open(filepath) as f:
for line in f:
print line.split(',')[1]

Related

Extract data from JSONs inside the CSV [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Could you advise what would be the best way to extract an element from JSON when I have multiple JSONs in a CSV file?
The data structure is like this
Line1: {"CreationTime":"2018-10-29T13:40:13","Id":"35127aae-a888-4665-6514-08d63da40e14","Operation":....,"IPaddress":"x.x.x.x"...}
Line 2: {"CreationTime":"2018-10-29T13:41:13","Id":"35127aae-a888-4665-6514-08d63da40e14","Operation":....,"IPAddress":"x.x.x.x"...}
.....
LineYY:...
My goal is to extract the IPAddress value from every line, that means from every json array. What would be the best way to do it?
Thanks a lot!
You can use json to load each line into a dictionary and then access IPAddress
import json
ips = []
for line in data.readlines():
row = json.loads(line)
ips.append(row['IPAddress'])

how to write regex to get only the users from this string output [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
consider below the output of my subprocess module of python. Now from here I want to grab the very first username only like root ,daemon and continuew. I tried but not able to write the exact regex to fetch the users only .
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
You do not a regex in the first place, as per your comments. So, you may iterate over the output line by line (str.splitlines()), split the line with : (str.split(':')) and take the first result (result[0]). This expects the output to be consistent, else it will fail.
Use split instead of regex
for line in output.split('\n'):
print line.split(':')[0]
>>>root
>>>daemon
....

Data extract from text file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Please help.
I have data in text file with 2 long columns tab spaced
I have to extract first and second columns separately
Can it be done with Python ?
If you need the columns as two separate lists:
first_column = []
second_column = []
data_file = open('your_file.txt')
for line in data_file.readlines():
first_column.append(line.split('\t')[0])
second_column.append(line.split('\t')[1])
data_file.close()

How to remove words from text file in Python that contain certain letter? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm new to Python and need to remove lines from text file in Python that contain a certain letter. i.e. a text file containing the months of the year and I want to remove the months containing an r. I'm completely lost.
How about this? (Reads from stdin, writes to stdout.)
import sys
for line in sys.stdin:
if 'r' not in line:
print(line)
Sample usage:
$ python program.py < months_file > months_without_r_file

Printing a Chosen Line from a File [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I use Python to print a line of my choosing from a file?
E.g. a file 10 lines long and I use sys.argv to provide a value for the line that I wish to print.
f = open('put_your_text_file.csv', 'r')
num = sys.argv[2]
for i, line in enumerate(f.readlines()):
if i == num:
print(line)

Categories

Resources