pandas 如何在python中将文本文件信息转换为 Dataframe

sdnqo3pr  于 2023-08-01  发布在  Python
关注(0)|答案(1)|浏览(151)
  1. Test case / Scenario:
  2. 4.SBI Savings account transaction
  3. Pre-requisities:
  4. Login to the net banking and send money successfully
  5. Steps Description:
  6. First launch the url and click on "Net Banking" button. Now click on "LOGIN" button add_text PERSONAL, Now click on "CONTINUE TO LOGIN" button. You will see a username field there now enter username, enter password and click "Login" button. Now enter one time password or OTP received to your mobile, click "submit" button. Now click on "Payments / Transfers" button, and click on "Quick Transfer (Without Adding Beneficiary)" option. Enter Beneficiary Name, enter Beneficiary Account Number, Re-enter Beneficiary Account Number, select "Within SBI" option, now enter the amount and also choose rent "Rent" option in purpose.Finally accept the terms & conditions by clicking on checkbox.
  7. Test case / Scenario:
  8. 5.SBI Savings account transaction
  9. Pre-requisities:
  10. Login to the net banking and send money successfully
  11. Steps Description:
  12. First launch the url and click on "Net Banking" button. Now click on "LOGIN" button add_text PERSONAL, Now click on "CONTINUE TO LOGIN" button. You will see a username field there now enter username, enter password and click "Login" button. Now enter one time password or OTP received to your mobile, click "submit" button. Now click on "Payments / Transfers" button, and click on "Quick Transfer (Without Adding Beneficiary)" option. Enter Beneficiary Name, enter Beneficiary Account Number, Re-enter Beneficiary Account Number, select "Within SBI" option, now enter the amount and also choose rent "Rent" option in purpose.Finally accept the terms and conditions by clicking on checkbox.

字符串
现在如何转换“测试用例/场景:”,“先决条件”,“步骤描述”,作为数据框列和“步骤描述”列数据应拆分成行,如果它们有“逗号”,“句号”,“和”。如何做我尝试并与此代码:

  1. import pandas as pd
  2. import re
  3. def split_steps_description(data):
  4. # Find the index of the "Steps Description" heading
  5. steps_index = data.index("Steps Description:") + 1
  6. # Get the content under the "Steps Description" heading
  7. steps_description = data[steps_index].strip()
  8. # Split the content based on commas, "and," or full stops
  9. steps = re.split(r',|\sand\s|\.', steps_description)
  10. # Clean up the steps (remove leading/trailing spaces)
  11. steps = [step.strip() for step in steps if step.strip()]
  12. return steps
  13. def text_input(file):
  14. # Read the text data from a file
  15. with open(file, "r") as f:
  16. data = f.read().splitlines()
  17. scenarios = []
  18. current_scenario = None
  19. for line in data:
  20. line = line.strip()
  21. if line.startswith("Test case / Scenario:"):
  22. current_scenario = line.replace("Test case / Scenario:", "").strip()
  23. elif line.startswith("Steps Description:"):
  24. steps = split_steps_description(data)
  25. # Add each split step as a separate row in the DataFrame
  26. for step in steps:
  27. scenarios.append({
  28. 'Test case / Scenario:': current_scenario,
  29. 'Steps': step
  30. })
  31. # Convert scenarios to DataFrame
  32. df = pd.DataFrame(scenarios)
  33. return df
  34. df = text_input('testing.txt')
  35. print(df)


我得到的输出是:

  1. Test case / Scenario: Steps
  2. 0 First launch the url
  3. 1 click on "Net Banking" button
  4. 2 Now click on "LOGIN" button add_text PERSONAL
  5. 3 Now click on "CONTINUE TO LOGIN" button
  6. 4 You will see a username field there now enter ...
  7. 5 enter password
  8. 6 click "Login" button
  9. 7 Now enter one time password or OTP received to...
  10. 8 click "submit" button
  11. 9 Now click on "Payments / Transfers" button
  12. 10 click on "Quick Transfer (Without Adding Benef...
  13. 11 Enter Beneficiary Name
  14. 12 enter Beneficiary Account Number
  15. 13 Re-enter Beneficiary Account Number
  16. 14 select "Within SBI" option
  17. 15 now enter the amount
  18. 16 also choose rent "Rent" option in purpose
  19. 17 Finally accept the terms & conditions by click...
  20. 18 First launch the url
  21. 19 click on "Net Banking" button
  22. 20 Now click on "LOGIN" button add_text PERSONAL
  23. 21 Now click on "CONTINUE TO LOGIN" button
  24. 22 You will see a username field there now enter ...
  25. 23 enter password
  26. 24 click "Login" button
  27. 25 Now enter one time password or OTP received to...
  28. 26 click "submit" button
  29. 27 Now click on "Payments / Transfers" button
  30. 28 click on "Quick Transfer (Without Adding Benef...
  31. 29 Enter Beneficiary Name
  32. 30 enter Beneficiary Account Number
  33. 31 Re-enter Beneficiary Account Number
  34. 32 select "Within SBI" option
  35. 33 now enter the amount
  36. 34 also choose rent "Rent" option in purpose
  37. 35 Finally accept the terms & conditions by click...


我期待这样的输出:

  1. Test case / Scenario: Steps
  2. 0 4.SBI Savings account transaction First launch the url
  3. 1 4.SBI Savings account transaction click on "Net Banking" button
  4. 2 4.SBI Savings account transaction Now click on "LOGIN" button add_text PERSONAL
  5. 3 4.SBI Savings account transaction Now click on "CONTINUE TO LOGIN" button


如何实现最终结果,请帮助我在这方面。谢谢。

t5fffqht

t5fffqht1#

  1. import pandas as pd
  2. import re
  3. lines = text.splitlines()
  4. df = pd.DataFrame({
  5. lines[0]: [lines[1]],
  6. "Steps": [re.split(r'\.|,|and', lines[5])]
  7. })
  8. df.explode('Steps')

字符串

相关问题