简体   繁体   English

如何读取文件的内容并将其转换为python中的字典

[英]how to read the contents of file and convert it into dictionary in python

I have a text file (applications.txt) containing n number of lines of data (with 2 delimiters) in the below format. 我有一个文本文件(applications.txt),包含以下格式的n行数据(带有2个分隔符)。

1) mytvScreen|mytvScreen|Mi TV,Mí TV, My Tv, TV 1)mytvScreen | mytvScreen | Mi TV,MíTV,我的电视,电视
2) watchNextScreen|watchNextScreen|Seguir viendo,Mi TV Seguir viendo 2)watchNextScreen | watchNextScreen | Seguir viendo,Mi TV Seguir viendo
3) recordingsScreen|recordingsScreen|Grabaciones,Mis Grabaciones,Mi TV 3)录音屏幕|录音屏幕| Grabaciones,Mis Grabaciones,Mi TV

Note: 1,2,3 are just line numbers for reference. 注意:1,2,3只是行号以供参考。 Original file doesn't contain the number. 原始文件不包含该号码。

I am trying to write a function that would read each line and convert it into a dictionary using the value before the first delimiter and the values after the second delimiter, like the example shown below. 我正在尝试编写一个函数,它将读取每一行并使用第一个分隔符之前的值和第二个分隔符之后的值将其转换为字典,如下所示的示例。

eg: 例如:

The below line one should be converted into dictionary as expected below. 下面的第一行应该转换成字典,如下所示。 1) mytvScreen|mytvScreen|Mi TV,Mí TV, My Tv, TV 1)mytvScreen | mytvScreen | Mi TV,MíTV,我的电视,电视

Expected Format: 预期格式:

mytvScreen : Mi TV, Mí TV, My Tv, TV mytvScreen:Mi TV,MíTV,我的电视,电视

Also, Upon giving any value which are comma separated, it should return the value before the colan . 此外,在给出任何逗号分隔的值时,它应该返回可乐之前的值。

Eg: 例如:

When the value Mi TV is given, it should return mytvScreen or for the other comma separated values also, it should return mytvScreen 当给出Mi TV的值时,它应该返回mytvScreen或其他逗号分隔值,它应该返回mytvScreen

I was able to read the file and print the values as expected. 我能够读取文件并按预期打印值。

But not sure how can i convert each line into a dictionary. 但不知道我怎样才能将每一行转换成字典。

with open('applications.txt') as f:
                for line in f:
                        details=line.split("|",2)
                        print (details[0] + ' : '+ details[2])

Your Help is highly appreciated. 非常感谢您的帮助。

you should be adding items to dictionary, one of the approach is given below. 你应该将项目添加到字典中,其中一种方法如下。

s_dict= dict()
with open('applications.txt') as f:
      for line in f:
          details=line.split("|",2)
          print (details[0] + ' : '+ details[2])
          s_dict[details[0]]  = details[2]
      print (s_dict)

I build a file-object to mimic applications.txt content: 我构建了一个文件对象来模仿applications.txt内容:

>>> import io
>>> f = io.StringIO("""mytvScreen|mytvScreen|Mi TV,Mí TV, My Tv, TV
... watchNextScreen|watchNextScreen|Seguir viendo,Mi TV Seguir viendo
... recordingsScreen|recordingsScreen|Grabaciones,Mis Grabaciones,Mi TV""")

Your file is obviously a csv file, thus you should use the csv module to parse it: 您的文件显然是一个csv文件,因此您应该使用csv模块来解析它:

>>> import csv
>>> reader = csv.reader(f, delimiter='|')

The last part is just a dict-comprehension: 最后一部分只是一个词典理解:

>>> {row[0]: row[2] for row in reader}
{'mytvScreen': 'Mi TV,Mí TV, My Tv, TV', 'watchNextScreen': 'Seguir viendo,Mi TV Seguir viendo', 'recordingsScreen': 'Grabaciones,Mis Grabaciones,Mi TV'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM