gpt4 book ai didi

python - 如何从只有一列(姓名、电话、地址)的 CSV 文件中提取数据

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

我有一个 CSV 文件。它只有一列。第 1 行 = 名称。第 2 行 = 电话。第 3-5 行 = 地址。

我尝试过用 Excel 进行组织,但我迷失了。我是初学者,希望学习!

Chris
778-222-2445
17 Main Street
Clinton, OH
9 Miles

John
772-022-2022
411 Oxford Street
Miami, FL
15 Miles

Chris 778-222-2445 17 Main Street Clinton, OH 9 Miles
John 772-022-2022 411 Oxford Street Miami, FL 15 Miles

等等...

最佳答案

此代码将您的文件转换为扁平的空格分隔行

with open('file.txt') as f:
record = []
for line in f: # iterate through file
line = line.strip() # remove useless endings
if line: # data on line?
record.append(line)
else: # empty line => next record
if record: # avoid printing if we had several empty lines
print (' '.join(record))
record = [] # create new record
# there could be no empty line at EOF
if record: # avoid printing if we had several empty lines
print (' '.join(record))

关于python - 如何从只有一列(姓名、电话、地址)的 CSV 文件中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54182450/

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