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)
Related
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 2 years ago.
Improve this question
I have a file contain key value pairs below form:
[
{
af_NA: "Afrikaans (Namibia)",
af_ZA: "Afrikaans (South Africa)",
af: "Afrikaans",
ak_GH: "Akan (Ghana)",
ak: "Akan",
sq_AL: "Albanian (Albania)",
sq: "Albanian",
am_ET: "Amharic (Ethiopia)",
...
}
]
but keys are not in quotes. I want put all keys in quotes using python code.
Thanks
this looks like valid yaml, so you could use PyYaml to parse the file, then dump it to json
with open('your_file') as fl:
data = yaml.safe_load(fl)
print(json.dumps(data))
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
Context: I want to use Python to iterate over a bunch of json documents (cosmodb) and remove the last portion of a string in the value of a key
I want to turn this:
"id": "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$1234$uspc02"
into this:
"id": "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$"
Any help would be greatly appreciated.
You could easily implement this with this piece of code:
result = my_string.rsplit('$', 1)[0] + "$"
Which behaves like this:
>>> my_string = "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$1234$uspc02"
>>> print(my_string.rsplit('_', 1)[0])
"randomstuff-channelruleprintermap-1234-$pc2$randomstuff$"
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 3 years ago.
Improve this question
these items are in a list
SF-04-08-010-MD01,
AHU-VVIP-02-003-MD03,
AHU-02-17-019-DPS03,
AHU-T3-01-PL-TS01,
EF-03-32-108-MD01,
AHU-02-16-019-MD01,
AHU-T3-01-003-MD01,
SF-04-08-010-MD01,
AHU-VVIP-02-003-MD03,
so i want a new list which should be like
SF-04-08
AHU-VVIP
AHU-02-17
AHU-T3-01
EF-03-32
AHU-02-16
AHU-T3-01
SF-04-08
AHU-VVIP-02
using python??
you can use strip like:
data = ['SF-04-08-010-MD01',
'AHU-VVIP-02-003-MD03',
'AHU-02-17-019-DPS03'
]
for item in data:
print ('-'.join(item.split('-')[:3]))
output:
SF-04-08
AHU-VVIP-02
AHU-02-17
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()