尝试 copy/paste 开始点和结束点之间的文本、转置和交换数据点
Trying to copy/paste text between Start and End points, transpose, and swap data points
我有一些代码可以将大文件 copy/paste 转换为我需要的已解析文件。这是一个工作脚本。
with open('C:\Users\Excel\Desktop\test_in.txt') as infile, open('C:\Users\Excel\Desktop\test_out.txt', 'w') as outfile:
copy = False
for line in infile:
if line.strip() == "Start":
copy = True
elif line.strip() == "End":
copy = False
elif copy:
outfile.write(line)
现在,我想弄清楚如何转置每个测试块,并多次交换相邻的数据点。也许这需要一个 dta 框架,我不太确定。
这是之前的图片。
这是一张后图。
这是我的示例文本。
file name
file type
file size
Start
- data_type: STRING
name: Operation
- data_type: STRING
name: SNL_Institution_Key
- data_type: INTEGER
name: SNL_Funding_Key
End
- data_type: STRING
name: Operation
- data_type: STRING
name: SNL_Institution_Key
- data_type: INTEGER
name: SNL_Funding_Key
Start
- data_type: STRING
name: SEDOL_NULL
- data_type: STRING
name: Ticker
- data_type: DATETIME
name: Date_of_Closing_Price
End
在我看来,这在 Python 中很难做到。如果做这一切太难了,请告诉我。 Python 可能不是完成这项工作的正确工具。我对 Python 的了解还不够,无法确定这是否是正确的方法。谢谢你的时间。
按冒号拆分行,然后按不同顺序合并它们。
我添加了一些标志来完全按照您的文件中的标点符号来实现,
但是对于中等大小的数据,我通常使用迭代的几个正则表达式或字符串替换
with open('C:\Users\Excel\Desktop\test_in.txt') as infile,
file_start = True
line = line.strip()
next(infile)
next(infile)
next(infile)
for line in infile:
if line.strip() == "Start":
if file_start:
file_start = False # write nothing first time
else:
outfile.write('\n')
line_start = True # starting new line in the output file
elif not line.strip() == "End":
if not line_start:
outfile.write(", ")
linestart = False
line = line.strip(" -")
s = line.split(": ")
outfile.write(": ".join(s[::-1]))
我有一些代码可以将大文件 copy/paste 转换为我需要的已解析文件。这是一个工作脚本。
with open('C:\Users\Excel\Desktop\test_in.txt') as infile, open('C:\Users\Excel\Desktop\test_out.txt', 'w') as outfile:
copy = False
for line in infile:
if line.strip() == "Start":
copy = True
elif line.strip() == "End":
copy = False
elif copy:
outfile.write(line)
现在,我想弄清楚如何转置每个测试块,并多次交换相邻的数据点。也许这需要一个 dta 框架,我不太确定。
这是之前的图片。
这是一张后图。
这是我的示例文本。
file name
file type
file size
Start
- data_type: STRING
name: Operation
- data_type: STRING
name: SNL_Institution_Key
- data_type: INTEGER
name: SNL_Funding_Key
End
- data_type: STRING
name: Operation
- data_type: STRING
name: SNL_Institution_Key
- data_type: INTEGER
name: SNL_Funding_Key
Start
- data_type: STRING
name: SEDOL_NULL
- data_type: STRING
name: Ticker
- data_type: DATETIME
name: Date_of_Closing_Price
End
在我看来,这在 Python 中很难做到。如果做这一切太难了,请告诉我。 Python 可能不是完成这项工作的正确工具。我对 Python 的了解还不够,无法确定这是否是正确的方法。谢谢你的时间。
按冒号拆分行,然后按不同顺序合并它们。 我添加了一些标志来完全按照您的文件中的标点符号来实现, 但是对于中等大小的数据,我通常使用迭代的几个正则表达式或字符串替换
with open('C:\Users\Excel\Desktop\test_in.txt') as infile,
file_start = True
line = line.strip()
next(infile)
next(infile)
next(infile)
for line in infile:
if line.strip() == "Start":
if file_start:
file_start = False # write nothing first time
else:
outfile.write('\n')
line_start = True # starting new line in the output file
elif not line.strip() == "End":
if not line_start:
outfile.write(", ")
linestart = False
line = line.strip(" -")
s = line.split(": ")
outfile.write(": ".join(s[::-1]))