首页 > 解决方案 > 使用python在索引位置重新格式化带有字符的txt文件

问题描述

非常新手程序员在这里问一个问题。我已经搜索了所有论坛,但找不到可以解决这个问题的东西,我认为会有一个简单的功能。有没有办法做到这一点?

我正在尝试重新格式化文本文件,以便可以将其与 pandas 函数一起使用,但这需要我的数据采用特定格式。

目前我的数据是以下格式的 txt 文件,其中包含超过 1000 行数据:

["01/09/21","00:28",7.1,75,3.0,3.7,3.7,292,0.0,0.0,1025.8,81.9,17.1,44,3.7,4.6,7.1,0,0,0.00,0.00,3.0,0,0.0,292,0.0,0.0]
    ["01/09/21","00:58",7.0,75,2.9,5.1,5.1,248,0.0,0.0,1025.9,81.9,17.0,44,5.1,3.8,7.0,0,0,0.00,0.00,1.9,0,0.0,248,0.0,0.0
    ]["01/09/21","01:28",6.9,74,2.6,4.1,4.1,248,0.0,0.0,1025.8,81.9,17.0,44,4.1,4.1,6.9,0,0,0.00,0.00,2.5,0,0.0,248,0.0,0.0

我需要它

["01/09/21","00:28",7.1,75,3.0,3.7,3.7,292,0.0,0.0,1025.8,81.9,17.1,44,3.7,4.6,7.1,0,0,0.00,0.00,3.0,0,0.0,292,0.0,0.0]
["01/09/21","00:58",7.0,75,2.9,5.1,5.1,248,0.0,0.0,1025.9,81.9,17.0,44,5.1,3.8,7.0,0,0,0.00,0.00,1.9,0,0.0,248,0.0,0.0]

这需要在逗号之前的日期["的开头添加一个"并在逗号之前添加一个,然后在逗号之后添加另一个""在时间部分的末尾添加另一个。在行尾,我还需要],在末尾添加一个。

我认为这样的事情会起作用,但是第二个括号出现在换行符之后(\n)有没有办法避免这种情况?

infile=open(infile)

outfile=open(outfile, 'w')


def format_line(line):
    elements = line.split(',') # break the comma-separated data up
    for k in range(2):
        elements[k] = '"' + elements[k] + '"' # put quotes around the first two elements
        print(elements[k])
    new_line = ','.join(elements) # put them back together
    return '[' + new_line + ']' # add the brackets

for line in infile:
    outfile.write(format_line(line))
outfile.close()

标签: pythoncsvindexingtxtreformatting

解决方案


您指的是在定义之前的函数。

format_line在 for 循环中调用它之前移动定义。

当我重新排列您的代码时,它似乎可以工作。

新代码:

outfile=open("outputfile","w")

def format_line(line):
    elements = line.split(',') # break the comma-separated data up
    for k in range(2):
        elements[k] = '"' + elements[k] + '"' # put quotes around the first two elements
    new_line = ','.join(elements) # put them back together
    return '[' + new_line + ']' # add the brackets

for line in infile:
    format_line(line)

推荐阅读