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()
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
this is how my .dat file looks like i want to know how to extract data from it like i want it like 1::Toy Story (1995) each thing in separate column. also i want to do it without pandas and numpy is there anyway possible
with open('ml-1m/movies.dat',encoding='iso-8859-1 ') as datFile:
print([data.split()[0] for data in datFile])
here is one way with result as a dictionnary
dict_of_film = {}
for i in open(r"path").readlines():
index,name,genre,_ = (i.replace("\n",'').split('::'))
dict_of_film[index] = { "name" : name , "genre" : genre }
print(dict_of_film)
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 1 year ago.
Improve this question
So i have a list with strings for path of images:
["folder1/_D7327", "folder1/_D7527", "folder2/_D7455", "folder3/_D1237"]
I want to automatically create a dictionary to store each photo:
{"folder1": ["_D7327", "_D7527"], "folder2": ["_D7455"], "folder3": ["_D1237"]}
How can I achieve something like this automatically?
You can use dict.setdefault. For example:
lst = ["folder1/_D7327", "folder1/_D7527", "folder2/_D7455", "folder3/_D1237"]
out = {}
for path in lst:
folder, file = path.split("/")
out.setdefault(folder, []).append(file)
print(out)
Prints:
{'folder1': ['_D7327', '_D7527'], 'folder2': ['_D7455'], 'folder3': ['_D1237']}
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 2 years ago.
Improve this question
Write python code to find the total number of null values on the excel file without using isnull function ( you should use loop statement)
Using for statement in dataframes is not a good practice. If you're doing only for challenge, this could help:
data = pd.read_excel('data.xlsx')
data.fillna(False, inplace=True)
amount_nan = 0
for column in data:
for value in data[column]:
if not value:
amount_nan = amount_nan + 1
print(amount_nan)
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)
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]