gpt4 book ai didi

python - 将重复的 "key=value"对文件读入 DataFrame

转载 作者:行者123 更新时间:2023-12-04 01:36:04 25 4
gpt4 key购买 nike

我有一个包含这种格式数据的 txt 文件。前 3 行一遍又一遍地重复。

name=1
grade=A
class=B
name=2
grade=D
class=A

我想以表格格式输出数据,例如:

name | grade | class
1 | A | B
2 | D | A

我正在努力设置标题并循环遍历数据。到目前为止我尝试过的是:
def myfile(filename):
with open(file1) as f:
for line in f:
yield line.strip().split('=',1)

def pprint_df(dframe):
print(tabulate(dframe, headers="keys", tablefmt="psql", showindex=False,))

#f = pd.DataFrame(myfile('file1')
df = pd.DataFrame(myfile('file1'))
pprint_df(df)

输出是

+-------+-----+
| 0 | 1 |
|-------+-----|
| name | 1 |
| grade | A |
| class | B |
| name | 2 |
| grade | D |
| class | A |
+-------+-----+

不是我正在寻找的。

最佳答案

此解决方案假定文本格式与您所描述的一样,但您可以修改它以使用不同的词来表示新行的开头。在这里,我们假设新行以 name 开头。 field 。我已经修改了你的 myfile()下面的函数,希望它给你一些想法:)

def myfile(filename):
d_list = []
with open(filename) as f:
d_line = {}
for line in f:
split_line = line.rstrip("\n").split('=') # Strip \n characters and split field and value.
if (split_line[0] == 'name'):
if d_line:
d_list.append(d_line) # Append if there is previous line in d_line.
d_line = {split_line[0]: split_line[1]} # Start a new dictionary to collect the next lines.
else:
d_line[split_line[0]] = split_line[1] # Add the other 2 fields to the dictionary.
d_list.append(d_line) # Append the last line.
return pd.DataFrame(d_list) # Turn the list of dictionaries into a DataFrame.

关于python - 将重复的 "key=value"对文件读入 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58832143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com