Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Python

tine

New Coder
LOINCDATA = """
lab item, result, reference range
ALK PHOS, 72, 35-100
BG RANDOM, 125 mg dl^-1, 70-125
BUN, 16 mg/dl, 7-25
CALCIUM, 9.6 mg/dl, 8.2-10.2
CHLORIDE, 101 mmol/l, 96-109
CO2, 27 mmol/l, 23-29
CREATININE, 0.7 mg/dl, 0.6-1.2
PO4, 2.9 mg/dl, 2.5-4.5
K, 4.5 mmol l^-1, 3.5-5.3
SGOT (AST), 31 U/L, 0-40
BILI TOTAL, 0.7 mg/dl, 0.0-1.3
URIC ACID, 4.8 mg/decilitre, 3.4-7.0
LDH TOTAL, 136 IU/Litre, 0-200
SODIUM, 135 mmol/liter, 135-145
"""


How can I add another string before each line ?
 
Hello this is Gulshan Negi
Well, you can try this code to get what you are looking for.

Code:
LOINCDATA = """
lab item, result, reference range
ALK PHOS, 72, 35-100
BG RANDOM, 125 mg dl^-1, 70-125
BUN, 16 mg/dl, 7-25
CALCIUM, 9.6 mg/dl, 8.2-10.2
CHLORIDE, 101 mmol/l, 96-109
CO2, 27 mmol/l, 23-29
CREATININE, 0.7 mg/dl, 0.6-1.2
PO4, 2.9 mg/dl, 2.5-4.5
K, 4.5 mmol l^-1, 3.5-5.3
SGOT (AST), 31 U/L, 0-40
BILI TOTAL, 0.7 mg/dl, 0.0-1.3
URIC ACID, 4.8 mg/decilitre, 3.4-7.0
LDH TOTAL, 136 IU/Litre, 0-200
SODIUM, 135 mmol/liter, 135-145
"""


# Split the string into lines
lines = LOINCDATA.strip().split("\n")


# Define the string to be added before each line
prefix = "PRE: "


# Add the prefix to each line
modified_lines = [prefix + line for line in lines]


# Join the modified lines back into a single string
modified_loincdata = "\n".join(modified_lines)


# Print the modified LOINCDATA
print(modified_loincdata)

Thanks
 
Hiya, Give this a try:-

To add another string before each line in the given LOINCDATA string, you can use the following Python code:

Python:
[CODE=python]LOINCDATA = """
lab item, result, reference range
ALK PHOS, 72, 35-100
BG RANDOM, 125 mg dl^-1, 70-125
BUN, 16 mg/dl, 7-25
CALCIUM, 9.6 mg/dl, 8.2-10.2
CHLORIDE, 101 mmol/l, 96-109
CO2, 27 mmol/l, 23-29
CREATININE, 0.7 mg/dl, 0.6-1.2
PO4, 2.9 mg/dl, 2.5-4.5
K, 4.5 mmol l^-1, 3.5-5.3
SGOT (AST), 31 U/L, 0-40
BILI TOTAL, 0.7 mg/dl, 0.0-1.3
URIC ACID, 4.8 mg/decilitre, 3.4-7.0
LDH TOTAL, 136 IU/Litre, 0-200
SODIUM, 135 mmol/liter, 135-145
"""

# Add a string before each line
prefix = "PREFIX: "
LOINCDATA_with_prefix = '\n'.join([prefix + line for line in LOINCDATA.strip().split('\n')])

print(LOINCDATA_with_prefix)
[/CODE]

This code will add the string "PREFIX: " before each line in the LOINCDATA string and print the updated string. The resulting output will have the prefix added to each line:

Code:
[CODE=python]PREFIX: lab item, result, reference range
PREFIX: ALK PHOS, 72, 35-100
PREFIX: BG RANDOM, 125 mg dl^-1, 70-125
PREFIX: BUN, 16 mg/dl, 7-25
PREFIX: CALCIUM, 9.6 mg/dl, 8.2-10.2
PREFIX: CHLORIDE, 101 mmol/l, 96-109
PREFIX: CO2, 27 mmol/l, 23-29
PREFIX: CREATININE, 0.7 mg/dl, 0.6-1.2
PREFIX: PO4, 2.9 mg/dl, 2.5-4.5
PREFIX: K, 4.5 mmol l^-1, 3.5-5.3
PREFIX: SGOT (AST), 31 U/L, 0-40
PREFIX: BILI TOTAL, 0.7 mg/dl, 0.0-1.3
PREFIX: URIC ACID, 4.8 mg/decilitre, 3.4-7.0
PREFIX: LDH TOTAL, 136 IU/Litre, 0-200
PREFIX: SODIUM, 135 mmol/liter, 135-145
[/CODE]
 
You could just use a text editor that supports regular expressions, such as TextPad, and replace "^" with the required string. Or, if there are just a small number of lines (you didn't say), just insert the string on the first line, then select/copy and repeatedly paste (CTRL-V) at the start of each line. Surely that would be less trouble than custom coding.
 
Back
Top Bottom