csv Pandas读取txt文件,并以文本形式显示

pinkon5k  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(185)

我有一个txt文件形式的数据集,看起来像这样:

beer_name: Legbiter
beer_id: 19827
brewery_name: Strangford Lough Brewing Company Ltd
brewery_id: 10093
style: English Pale Ale
abv: 4.8
date: 1357729200
user_name: AgentMunky
user_id: agentmunky.409755
appearance: 4.0
aroma: 3.75
palate: 3.5
taste: 3.5
overall: 3.75
rating: 3.64
text: Poured from a 12 ounce bottle into a pilsner glass.A: A finger of creamy head with clear-dark amber body.S: Rich brown sugar. Malty...T: Slight sugars, dry malt, vague hops. Big malty-brown with sugar.M: Dry and slightly astringent before a boring endtaste.O: Solid beer. Drinkable and interesting. Still vaguely bland.
review: True

字符串
我正在使用下面的函数来尝试将其转换为正确的df(之后进行了更多的处理,但这就是它抛出错误的地方):

rb_file_data = pd.read_csv(os.path.join(MATCHED_BEER_DIR, 'ratings_with_text_rb.txt'), sep=":", header=None, names=["Key", "Value"])


我遇到的问题是,一些评论在文本部分使用“:“(我特意选择了一个包含一些),这会引发以下错误:

ParserError: Error tokenizing data. C error: Expected 2 fields in line 34, saw 7


如果需要的话,我有足够的数据来摆脱整个评论,但如果可能的话,我很乐意保留它。
有没有一种方法可以只在分隔符第一次出现在一行中时使用它?

zpqajqem

zpqajqem1#

类似于https://stackoverflow.com/a/54504598/17142551。您可以使用:用途:

rb_file_data = pd.read_csv('ratings_with_text_rb.txt'), sep='^([^:]+): ', engine='python', usecols=['beer_name', 'Legbiter'])

字符串

gzjq41n4

gzjq41n42#

你可以试试下面的代码

import pandas as pd
import os

MATCHED_BEER_DIR = "give your directory path"

with open(os.path.join(MATCHED_BEER_DIR, 'ratings_with_text_rb.txt'), 'r') as file:
    lines = file.readlines()

data = [line.strip().split(':', 1) for line in lines]

rb_file_data = pd.DataFrame(data, columns=["Key", "Value"])

rb_file_data['Value'] = rb_file_data['Value'].str.strip()

print(rb_file_data)

字符串

6rvt4ljy

6rvt4ljy3#

你可以试试这个:

df = (pd.read_csv("ratings_with_text_rb.txt",  header=None, engine="python",
                  sep=r"(.+?):\s*(.+)") # Click here to see the regex-demo
            [[1, 2]].set_index(1).T) # the transpose is optional, maybe !

字符串
如果你有17个条目的块(* 这是最有可能的情况 *),你可以用途:

N = 17

tmp = pd.read_csv("ratings_with_text_rb.txt", header=None,
                  engine="python", sep=r"(.+?):\s*(.+)")[[1, 2]]

out = tmp.set_axis(tmp.index // N).set_index(1, append=True)[2].unstack(1)


输出量:

print(df)

1 beer_name beer_id         brewery_name  ... rating                 text review
0  Legbiter   19827  Strangford Lough...  ...   3.64  Poured from a 12...   True

[1 rows x 17 columns]

相关问题