简体   繁体   English

如何正确地从 .txt 文件中提取列名

[英]How to extract column names from .txt file properly

I am using the below-mentioned code in order to print the column names of the dataset ( .txt file ) but it prints whole rows.我正在使用下面提到的代码来打印数据集的列名.txt 文件),但它会打印整行。

import pandas as pd

df = pd.read_csv("HotelRec100.txt")
print(df.columns)

The result is here!结果就在这里!

a few rows from the data:数据中的几行:

"{""hotel_url"": ""Hotel_Review-g194775-d1121769-Reviews-Hotel_Baltic-Giulianova_Province_of_Teramo_Abruzzo.html"", ""author"": ""Lagaiuzza"", ""date"": ""2016-01-01T00:00:00"", ""rating"": 5.0, ""title"": ""Baltic, what else?"", ""text"": ""We have spent in this hotel our summer holidays both in summer 2014 and 2015- I was with my husband and my child ( 4 years old at present). I do really recommend this place- Staff si high qualified, Kind and really helpful- Animation staff get You involved, but always with discrection - Miniclub si super and activities offered are interesting and smart- Rooms clean, with AC and balcony- Restaurant offers a great selection of food - always. The beach si extremly closed to the hotel - Miniclub area offers some gazebos to have shade for kids- A lot of bicycles are available for free- I am completely satisfied of this hotel- Go in lime this!"", ""property_dict"": {}}" "{""hotel_url"": ""Hotel_Review-g194775-d1121769-Reviews-Hotel_Baltic-Giulianova_Prov "{""hotel_url"": ""Hotel_Review-g194775-d1121769-Reviews-Hotel_Baltic-Giulianova_Province_of_Teramo_Abruzzo.html"", ""author"": ""Lagaiuzza"", ""date"": ""2016-01- 01T00:00:00"", ""rating"": 5.0, ""title"": ""Baltic, 还有什么?"", ""text"": ""我们在这家酒店度过了我们的暑假在 2014 年和 2015 年夏天-我和我丈夫和我的孩子(目前 4 岁)在一起。我真的很推荐这个地方-员工素质高,友善且非常乐于助人-动画工作人员让您参与其中,但始终谨慎-超级迷你俱乐部和提供的活动既有趣又智能- 房间干净,有空调和阳台- 餐厅提供多种食物- 总是。海滩非常靠近酒店- 迷你俱乐部区提供一些凉亭为孩子们提供阴凉处- A很多自行车都可以免费使用-我对这家酒店非常满意-喜欢这个!"", ""property_dict"": {}}" "{""hotel_url"": ""Hotel_Review-g194775-d1121769-点评-Hotel_Baltic-Giulianova_Prov ince_of_Teramo_Abruzzo.html"", ""author"": ""ashleyn763"", ""date"": ""2014-10-01T00:00:00"", ""rating"": 5.0, ""title"": ""Excellent in every way!"", ""text"": ""I visited Hotel Baltic with my husband for some bike riding in the area, thinking it would just be another hotel. ince_of_Teramo_Abruzzo.html"", ""author"": ""ashleyn763"", ""date"": ""2014-10-01T00:00:00"", ""rating"": 5.0, ""title" ": ""各方面都很棒!"", ""text"": ""我和我丈夫一起去波罗的海酒店在该地区骑自行车,以为这只是另一家酒店。 I was so wrong.我错了。 We don't have children, but were so amazed at the attention to detail and kindness we experienced from every member of the staff.我们没有孩子,但我们对每位员工对细节和善意的关注感到非常惊讶。 It was truly amazing."", ""property_dict"": {""service"": 5.0, ""location"": 5.0, ""value"": 5.0}}" "{""hotel_url"": ""Hotel_Review-g194775-d1121769-Reviews-Hotel_Baltic-Giulianova_Province_of_Teramo_Abruzzo.html"", ""author"": ""DavideMauro"", ""date"": ""2014-08-01T00:00:00"", ""rating"": 5.0, ""title"": ""The house of your family's holiday"", ""text"": ""I've travelled quite a numbers of hotels but this is the best place you can achieve with an excellent ratio quality/money.真是太棒了。"", ""property_dict"": {""service"": 5.0, ""location"": 5.0, ""value"": 5.0}}" "{""hotel_url"": " "Hotel_Review-g194775-d1121769-Reviews-Hotel_Baltic-Giulianova_Province_of_Teramo_Abruzzo.html""""author"":""DavideMauro""""date"":""2014-08-01T00:00:00""" "rating"": 5.0, ""title"": ""你家度假的房子"", ""text"": ""我去过很多酒店,但这是你能达到的最好的地方具有出色的质量/金钱比率。 The equipe is really excellent.装备真的很棒。 The restaurant's staff and the chef are perfect.餐厅的员工和厨师都很完美。 Menu is always varying.菜单总是变化的。 Bar service is really fantastic.酒吧服务真的很棒。 On the beach rather than in the hotel, anything is perfect and our holiday went like a dream.在海滩上而不是在酒店里,一切都很完美,我们的假期就像一场梦。 Although prices could seems quite high, you must consider that you could even forget your wollet at home.虽然价格可能看起来相当高,但您必须考虑到您甚至可能将您的钱包忘在家里。 You'll never be required to spend any money.你永远不会被要求花任何钱。 Kids are always happy and miniclub staff is really efficient.孩子们总是很开心,迷你俱乐部的工作人员非常有效率。 My daughter crying for our leaving could explain better what I'm writing."", ""property_dict"": {""service"": 5.0, ""cleanliness"": 5.0, ""sleep quality"": 5.0}}"我女儿为我们的离开而哭泣可以更好地解释我在写什么。"", ""property_dict"": {""service"": 5.0, ""cleanliness"": 5.0, ""sleep quality"": 5.0} }"

Is there anyone who can help me to solve the issue?有没有人可以帮我解决这个问题?

Your .txt file does not have a column.您的.txt文件没有列。 When loaded into pandas, the first record is taken as column name.当加载到 pandas 中时,第一条记录被作为列名。 Use below to avoid it.使用下面来避免它。

df = pd.read_csv('HotelRec100.txt', header = None)

You can give any column name by using df.columns = ['MY COLUMN']您可以使用df.columns = ['MY COLUMN']给出任何列名

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

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