简体   繁体   English

如何去除 python 中字典列表值中的“\n”?

[英]How to strip '\n' in value of list of dictionaries in python?

I have a list of dictionaries that looks like this.我有一个看起来像这样的字典列表。

[{'title': 'Great Customer Service\n', 'name': 'John\n', 'date': '2017-12-21\n', 'feedback': 'The customer service here is very good. They helped me find a 2017 Camry with good condition in reasonable price. Campared to other dealers, they provided the lowest price. Definttely recommend!\n'}, {'title': 'You will find what you want here\n', 'name': 'Tom\n', 'date': '2019-06-05\n', 'feedback': "I've being look around for a second handed Lexus RX for my family and this store happened to have a few of those. The experience was similar to most car dealers. The one I ended up buying has good condition and low mileage. I am pretty satisfied with the price they offered.\n"}, {'title': 'Good deal for a 2015 RAV4\n', 'name': 'Anonymous\n', 'date': '2018-04-17\n', 'feedback': 'Called them to look for a second-hand RAV4 and they are very nice and patience to help me find me a few matches then scheduled an appointmet with me. Came in and they had everything ready for me. I was surprised how professional those sales are and they explained and answered all my questions. Ended up buying the car and been using it for more than a month now. Everything looks good!\n'}, {'title': 'Best experience so far\n', 'name': 'Katie\n', 'date': '2019-12-09\n', 'feedback': "My friend recommended this place and  I decided to give a try. I called them beforehand and made sure they have the models I was looking for. They evenly sent me a bunch of photos and specs of the car I was looking for. One the day I visited, the sales person John helped me with test drive and showed me what I should be aware of when buying a used car. I felt like I learned a lot. They made the process so smooth that I saved a lot of time. It's the best car purchasing experience I had so far!\n"}, {'title': 'Waste of my time\n', 'name': 'Anonymous\n', 'date': '2018-09-21\n', 'feedback': "I came in around 6pm and they seemed about to close the store. One of the sales seemed not being patient with me and made me feel like I have to either buy a car or come back later. Of course I didn't buy a car there. Hopefully they can treat every customer with more patience.\n"}]

I want to strip '\n' in every values of the dictionary.我想在字典的每个值中删除 '\n' 。 How can i do that?我怎样才能做到这一点? My code:我的代码:

template = ['title','name','date','feedback']
list_dict = []
for file in os.listdir():
    if file != 'script.py':
        with open (file, 'r') as f:
            reader = f.readlines()
            dicty = dict(zip(template,reader))
            list_dict.append(dicty)

print(list_dict)

Use list comprehension , dict comprehension and str.rstrip :使用list comprehensiondict comprehensionstr.rstrip

[{key: value.rstrip("\n") for key, value in a_dict.items()} for a_dict in list_dict]

Using pandas:使用 pandas:

import pandas as pd
df  = pd.DataFrame(dict)
df1 = pd.DataFrame()
for i, col in df.iteritems():
    for j in range(len(col)):
        df1.loc[j,i] = col[j][:-1]
df1.to_dict()

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

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