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

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

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:

  1. movie = "title = matrix, genre = action, year = 2000, rate = 8"
  2. movie = {
  3. "title": "matrix",
  4. "genre": "action",
  5. "year": "1964",
  6. "rate":"8"
  7. }

答案1

得分: 1

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

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

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

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

答案2

得分: 1

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

  1. import re
  2. input_user = "title = matrix, genre = action, year = 2000, rate = 8"
  3. # 创建用于匹配键值对的模式
  4. pattern = re.compile(r"(\w+) = ([\w,]+)" )
  5. # 在输入字符串中查找所有匹配项
  6. matches = pattern.findall(input_user)
  7. # 将匹配项转换为字典
  8. result = {key: value for key, value in matches}
  9. print(result)

结果:

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

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

英文:

For the solution you can use regex

  1. import re
  2. input_user = "title = matrix, genre = action, year = 2000, rate = 8"
  3. # Create a pattern to match the key-value pairs
  4. pattern = re.compile(r"(\w+) = ([\w,]+)" )
  5. # Find all matches in the input string
  6. matches = pattern.findall(input_user)
  7. # Convert the matches to a dictionary
  8. result = {key: value for key, value in matches}
  9. print(result)

The result:

  1. {'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

英文:
  1. movie = "title = matrix, genre = action, year = 2000, rate = 8"
  2. dict_all_movies = {}
  3. for idx in df.index:
  4. str_movie = df.at[idx, str_movie_column]
  5. movie_dict = dict(item.split(" = ") for item in str_movie.split(", "))
  6. 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:

确定