1 回答
TA贡献1810条经验 获得超4个赞
示例文本:
FullName;ISO3;ISO1;molecular_weight
Alanine;Ala;A;89.09
Arginine;Arg;R;174.20
Asparagine;Asn;N;132.12
Aspartic_Acid;Asp;D;133.10
Cysteine;Cys;C;121.16
基于“;”创建列 分隔器:
import pandas as pd
f = "aminoacids"
df = pd.read_csv(f,sep=";")
编辑:考虑到评论,我认为文本看起来更像是这样的:
t = """1234; text in written from with multiple sentences going over multiple lines until at some point the next ID is written dwon 2345; then the new Ad-Text begins until the next ID 3456; and so on1234; text in written from with multiple """
在这种情况下,像这样的正则表达式会将您的字符串拆分为 id 和文本,然后您可以使用它们来生成 pandas 数据框。
import re
r = re.compile("([0-9]+);")
re.split(r,t)
输出:
['',
'1234',
' text in written from with multiple sentences going over multiple lines until at some point the next ID is written dwon ',
'2345',
' then the new Ad-Text begins until the next ID ',
'3456',
' and so on',
'1234',
' text in written from with multiple ']
编辑2:这是对评论中提问者附加问题的回应: 如何将此字符串转换为具有 2 列的 pandas 数据框:ID 和文本
import pandas as pd
# a is the output list from the previous part of this answer
# Create list of texts. ::2 takes every other item from a list, starting with the FIRST one.
texts = a[::2][1:]
print(texts)
# Create list of ID's. ::1 takes every other item from a list, starting with the SECOND one
ids = a[1::2]
print(ids)
df = pd.DataFrame({"IDs":ids,"Texts":texts})
添加回答
举报