【发布时间】:2020-06-04 04:22:23
【问题描述】:
我有一个带有分隔符的超级凌乱的 .txt 文件 |我想读入python。现在,它的结构是这样的:
Name: Name1
Location: Location1
Address: Address1
...
|
Name: Name2
Location: Location2
Address: Address2
...
|
Name: Name3
Location: Location3
Address: Address3
...
|
等等。 每个条目有 164 个“列”,整个内容现在大约有 250 万行。我需要用分隔符分隔这些条目 |以我猜的垂直方式?然后找出一种方法将列名分配为列名 once 并包含其下方的所有条目。
到目前为止我写的:
import pandas as pd
extract = pd.read_csv(r'myfile',
encoding='utf8', sep='\|', index_col=False, names=['A'])
extract = extract.dropna()
to_drop = ["way too many strings aka columns I don't need because I couldn't figure out how to automate this"]
extract = extract[~extract['A'].str.contains('|'.join(to_drop))]
print(extract)
这会打印出我的专栏充满了废话,但我不知道如何做重要的“将内容分成列”位。
感谢您的帮助!
【问题讨论】:
-
请包括您的预期输出