将复杂的逗号分隔字符串转换为Python字典

huangapple go评论56阅读模式
英文:

Convert complex comma-separated string into Python dictionary

问题

我从Pandas的CSV文件中获得了以下字符串格式:

> "title = matrix, genre = action, year = 2000, rate = 8"

如何将该字符串值更改为如下的Python字典:

movie = "title = matrix, genre = action, year = 2000, rate = 8"

movie = {
"title": "matrix",
"genre": "action",
"year": "2000",
"rate": "8"
}

英文:

I am getting following string format from csv file in Pandas

> "title = matrix, genre = action, year = 2000, rate = 8"

How can I change the string value into a python dictionary like this:

movie = "title = matrix, genre = action, year = 2000, rate = 8" 

movie = {
   "title": "matrix",   
   "genre": "action",   
   "year": "1964", 
   "rate":"8" 
}

答案1

得分: 1

你可以将字符串拆分,然后将其转换为字典。
以下是示例代码

movie = "title = matrix, genre = action, year = 2000, rate = 8"
movie = movie.split(",")
# print(movie)
tempMovie = [i.split("=") for i in movie]
movie = {}
for i in tempMovie:
    movie[i[0].strip()] = i[1].strip()
print(movie)
英文:

You can split the string and then convert it into a dictionary.
A sample code is given below

movie = "title = matrix, genre = action, year = 2000, rate = 8"
movie = movie.split(",")
# print(movie)
tempMovie = [i.split("=") for i in movie]
movie = {}
for i in tempMovie:
    movie[i[0].strip()] = i[1].strip()
print(movie)

答案2

得分: 1

以下是翻译好的代码部分:

import re

input_user = "title = matrix, genre = action, year = 2000, rate = 8"

# 创建用于匹配键值对的模式
pattern = re.compile(r"(\w+) = ([\w,]+)" )

# 在输入字符串中查找所有匹配项
matches = pattern.findall(input_user)

# 将匹配项转换为字典
result = {key: value for key, value in matches}

print(result)

结果:

{'title': 'matrix,', 'genre': 'action,', 'year': '2000,', 'rate': '8'}

希望这可以解决您的问题。

英文:

For the solution you can use regex

import re

input_user = "title = matrix, genre = action, year = 2000, rate = 8"

# Create a pattern to match the key-value pairs
pattern = re.compile(r"(\w+) = ([\w,]+)" )

# Find all matches in the input string
matches = pattern.findall(input_user)

# Convert the matches to a dictionary
result = {key: value for key, value in matches}

print(result)

The result:

{'title': 'matrix,', 'genre': 'action,', 'year': '2000,', 'rate': '8'}

I hope this can solve your problem.

答案3

得分: 0

movie = "title = matrix, genre = action, year = 2000, rate = 8"

dict_all_movies = {}

for idx in df.index:
str_movie = df.at[idx, str_movie_column]
movie_dict = dict(item.split(" = ") for item in str_movie.split(", "))
dict_all_movies[str(idx)] = movie_dict

英文:
movie = "title = matrix, genre = action, year = 2000, rate = 8" 

dict_all_movies = {}

for idx in df.index:
    str_movie = df.at[idx, str_movie_column]
    movie_dict = dict(item.split(" = ") for item in str_movie.split(", ")) 
    dict_all_movies[str(idx)] = movie_dict

huangapple
  • 本文由 发表于 2023年2月8日 10:47:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定