Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python读取文本文件并传输到字典?_Python_Dictionary_Text_File Read - Fatal编程技术网

如何使用Python读取文本文件并传输到字典?

如何使用Python读取文本文件并传输到字典?,python,dictionary,text,file-read,Python,Dictionary,Text,File Read,我正在尝试读取一个类似于上面的txt文件,我想将其传输到字典中。 问题作为关键,然后是四个评论作为价值观 1. How do you like this product? As someone who wears glasses for distance, but not contacts I have struggled with vision on the slopes At this price, I'm very happy with the B1. They were very com

我正在尝试读取一个类似于上面的txt文件,我想将其传输到字典中。 问题作为关键,然后是四个评论作为价值观

1. How do you like this product?
As someone who wears glasses for distance, but not contacts I have struggled with vision on the slopes
At this price, I'm very happy with the B1. They were very comfortable
I wore these all winter for 10 weeks of skiing!
These are cheap in price yes but they do what they're supposed to
2. Do you have any recommendations?
The product is a defect, and the quality was bad
Yes, I like this product and it didn't fog up when I was skiing
I won't refer my friend to buy this product.
So far so good

感谢您的帮助

这取决于您的问题标准。这里我假设一个问题以

{"1. How do you like this product?":["As someone who wears glasses for distance, but not contacts I have struggled with vision on the slopes","At this price, I'm very happy with the B1. They were very comfortable","I wore these all winter for 10 weeks of skiing!","These are cheap in price yes but they do what they're supposed to","As someone who wears glasses for distance, but not contacts I have struggled with vision on the slopes"],"2. Do you have any recommendations?":["The product is a defect, and the quality was bad","Yes, I like this product and it didn't fog up when I was skiing","I won't refer my friend to buy this product.","So far so good"]
d = {}
with open('file') as file:
    lines = file.readlines()
    key = None
    for line in lines:
        if line[0].isdigit():
            key = line
            d[line] = []
        else:
            d[key].append(line)

不是很优雅,但此代码可以工作:

with open('input_file.txt') as f:
  out_dict = {}
  for line in f.readlines():
    # strip out blank characters at the beginning and the end
    line = line.strip()

    # a question ends with ?
    if line[:-1] == '?':
       current_key=line
       out_dict[current_key] = []
    else:
       out_dict[current_key].append(line)

你试过代码了吗?请包括你在这方面的最佳尝试,并说明你在哪一步被卡住了。谢谢。嗨,库巴,谢谢你的帮助。这正是我需要的!谢谢你的解决方案。实际上,并不是所有的问题都以“?”结尾。
outputDict = {}
with open('sampleText', 'r') as f:
    tempKey = None
    templist = []
    for line in f.readlines():
        if line[0].isdigit() and line[1] == '.':
            if tempKey != None:
                outputDict[tempKey] = templist
                templist = []
                tempKey = line.rstrip('\n')
                print('found a key that is not the first')
                print(line)
                print(tempKey)
            else:
                tempKey = line.rstrip('\n')
                print('found first key')
                print(line)
                print(tempKey)
        else:
            templist.append(line.rstrip('\n'))
            print('found val')
            print(line)
    outputDict[tempKey] = templist