在数据清理之前分割多行文本。分割 Oracle 用户表单数据的选项是什么?

问题描述 投票:0回答:1

我是一名医生,刚刚开始使用 Python,所以如果我误用了一些术语,请原谅。 我们的电子健康系统使用Oracle 用户表单。 血压数据显示在文本字段中,最多可包含 650 个单独的血压测量值。 这是 4 个测量值的示例:

 = 01-02-2024 16:05 - A. I AHMED
    128 / 84 
 = 01-02-2024 08:07 - G. I AL KHAIR
    122 / 80 
 = 01-02-2024 00:48 - E. H AL MALKY
    122 / 80 
 = 31-01-2024 00:48 - M. SHIBI
    124 / 

此数据的主要问题是无法直接分析它,因为它是一个字符串。 分析,例如平均值或终端数字偏好,需要清理数据,日期为datetime.date,时间为datetime.time,血压为Integer...

看来,在清理数据并将每种类型的数据存储到变量(日期、时间、护士姓名、收缩压、舒张压)之前,我可以(?)/必须(?)parsesplit这个多行数据分成单独的行。

我试过这个:

data = """
= 23-10-2023 01:43 - M. M AL FAKEEH
    119 / 79 
= 03-10-2023 23:49 - H. I ADAM
112 /
= 03-10-2023 23:49 - H. I ADAM
/ 70
= 03-10-2023 16:32 - S. M JEEZ
/ 76
= 03-10-2023 16:32 - S. M JEEZ
120 / 
= 03-10-2023 08:54 - S. I YOUNIS
122 / 81
"""

# Split the data into lines
lines = data.split('\n')

raw_BP_data_odd_lines = []
raw_BP_data_even_lines = []
for i, line in enumerate(lines):
    if i % 2 == 0:
        raw_BP_data_even_lines.append(line)
    else:
        raw_BP_data_odd_lines.append(line)

# Split the data inside raw_BP_data_odd_lines and raw_BP_data_even_lines
odd_lines_split = [line.split() for line in raw_BP_data_odd_lines]

even_lines_split = [line.split() for line in raw_BP_data_even_lines]
     #This split the data in each line into separate words

print("Odd lines:")
#for line in odd_lines_split:
for line in raw_BP_data_odd_lines:
    print(line)  # This means: Print every line in the variable "odd_lines_split"

print("Even lines:")
#for line in even_lines_split:
for line in raw_BP_data_even_lines:
    print(line)

结果在“偶数行:

”之后包含意外的空行
Odd lines:
= 23-10-2023 01:43 - M. M AL FAKEEH
= 03-10-2023 23:49 - H. I ADAM
= 03-10-2023 23:49 - H. I ADAM
= 03-10-2023 16:32 - S. M JEEZ
= 03-10-2023 16:32 - S. M JEEZ
= 03-10-2023 08:54 - S. I YOUNIS


Even lines:

    119 / 79
112 /
/ 70
/ 76
120 /
122 / 81

我猜问题在于选择“ " 分割行时的分隔符。

所以我决定尝试将剪贴板中的血压数据粘贴到变量中,希望能解决意外空间的问题。但输出却更糟(根本没有打印任何内容):

import pyperclip

# Paste data from the clipboard into a variable
clipboard_data = pyperclip.paste()

# Check if clipboard data is empty
if not clipboard_data:
    print("No data found in the clipboard.")
else:
    # Split the data into lines
    lines = clipboard_data.split('\n')

我在这个论坛上读到了关于使用 os.linesep

import os
import pyperclip
text=pyperclip.paste()     
                             #paste will paste a big string of text in 'text' string

# Separate lines and add stars
lines = text.split(os.linesep)

os.linesep 是避免不需要的空行的最佳选择吗?

配件问题: 为了清理血压数据并将其分为其中包含的 4 种不同的数据类型,我是否走在正确的轨道上?

python-3.x oracle split data-cleaning
1个回答
0
投票
我自己的部分答案。

os.linesep 似乎是一个有效的选项(是否是最佳选项,我不知道): 1.它将数据存储在变量中分离成行; 2.它确实删除了偶数行中的不需要的额外行

这是代码的初始部分:

import pyperclip import os # Paste data from the clipboard into a variable clipboard_data = pyperclip.paste() # Split the data into lines lines = clipboard_data.split(os.linesep) ''' Store odd lines (line 1, 3, 5...) in raw_BP_data_odd_lines and even lines (line 2, 4, 6, 8...) in raw_BP_data_even_lines''' raw_BP_data_odd_lines = [] raw_BP_data_even_lines = [] for i, line in enumerate(lines): if i % 2 == 0: raw_BP_data_even_lines.append(line) else: raw_BP_data_odd_lines.append(line) # Split the data inside raw_BP_data_odd_lines and raw_BP_data_even_lines odd_lines_split = [line.split() for line in raw_BP_data_odd_lines] even_lines_split = [line.split() for line in raw_BP_data_even_lines]
至于我是否“

在正确的轨道上清理血压数据并将其分成它包含的4种不同的数据类型”,我会继续尝试,直到找到解决方案。那我就在这里回答吧

© www.soinside.com 2019 - 2024. All rights reserved.