Managing imported text in Python: techniques and methods

My text file has the following format:

1, blabal.1    
2, bal,abla2
3, bal,a.bla3

I am looking to extract the numbers and texts into separate variables. How can I achieve this?

number_list = [1, 2, 3]
texts = ["balabal1", "balabal2", "balabal3"]

Answer №1

numbers = list()
sentences = list()
with open("data.txt") as file:
    for row in file:
        num, sentence = row.strip().split(", ")
        numbers.append(int(num))
        sentences.append(sentence.replace(".", "").replace(",", ""))

Answer №2

To extract the data, loop through the lines and use the split function with a delimiter of a comma (,). After splitting, add the first element (the number) to a list called numb_list, making sure to convert it to an integer using int(). Then add the second element (accessed by index 1) to another list called text, removing any leading or trailing spaces.

numb_list = []
text = []

with open("file.txt") as data_file:
    for line in data_file:
        parts = line.split(",")
        numb_list.append(int(parts[0]))
        text.append(parts[1].strip())

Answer №3

Check out this alternative approach using the Pandas library:

import pandas as pd

data = pd.read_csv('c:/folder/data.csv', header=None)

nums = data.iloc[:,0].tolist()
texts = data.iloc[:,1].tolist()

Result

[4, 5, 6]

['text1', 'text2', 'text3']

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm looking for the location of WidgetRedirector within Python3. Where should I start searching

I initially had success using a tkform library in Python2, which worked flawlessly. However, upon transitioning to Python3, I encountered issues running it, specifically with the line: from idlelib.WidgetRedirector import WidgetRedirector It appears that ...

What is the process for assigning an ID to an object by clicking on an adjacent link in Django?

Trying to set an identifier for a CSV file's name so that clicking on a link next to it will lead to viewing the file itself on another webpage. Encountering the error message 'QueryDict' object has no attribute 'objects' Please ...

Prompt for Map-Reduce utilizing RetrievalQA Chain

Here is a demonstration of how I constructed my RAG model using the ParentDocumentRetriever from Langchain with Memory. Currently, I am utilizing the RetrievalQA-Chain with the default chain_type="stuff". However, I am interested in experimenting ...

Select the checkbox located beside the dropdown option using Selenium with Python

Looking to select the checkbox for a specific date from a dropdown list https://i.stack.imgur.com/D0aG1.png I have included some code and a screenshot, but not sure if more code is needed for assistance. <div class="row" role="none" ...

What is the process for implementing the Google Analytics tag on a Streamlit-built website?

I want to monitor my Streamlit UI in Google Analytics. To achieve this, Google Analytics requires the following code snippet to be inserted into the <head> section of the HTML file. <script async src="https://www.googletagmanager.com/gtag/js? ...

The md5Checksum property of the Google Drive API is unable to be utilized in Python

I've been working on a script to identify duplicate files using 'md5Checksum', but I keep encountering a KeyError. Can you confirm if this method is supposed to work as intended? Appreciate your help. FILES = SERVICE.children().list(folder ...

Enhancing Detection with OpenCV Background Subtraction

I have a system in place that can detect eggs on a conveyor belt, but the issue arises when there are no eggs present as the algorithm still identifies objects. List of detected eggs on the conveyor: https://i.stack.imgur.com/4nW8G.png https://i.stack.im ...

Associating a function with a variable in Python

Hey there, I'm a bit stuck on this snippet of code: def first(): print('Hello From First') def wrapper(): print('Hi From Second') return wrapper first() # a = first() # a() This is the output I got: Hello F ...

Postman - Setting up dynamic variables using response data

Like many of you, I am always looking for ways to simplify my work :) The issue at hand is whether it is feasible to establish a variable in Postman based on the response value from my application backend. Here's the rundown: My application generate ...

Is there a way to sort and group unique values when they are in a list format?

If I were to calculate the mean of the last 2 values based on the column id, I would use the following code snippet: df['rolling_mean_2'] = df.groupby('id').apply(lambda x: x.rolling(2, min_periods=2).mean()) >> id value ...

Prevent all employees from being re-spawned simultaneously

Running uwsgi with the max-requests=15000 parameter ensures that each worker is recycled after processing 15,000 requests. To prevent multiple workers from being recycled simultaneously when they reach the maximum number of requests at the same time, the m ...

Troubleshooting Encoding Issues in Docker When using Ubuntu Containers (Python, Java, Ruby, etc.) with Various Encodings (ASCII, UTF-8)

While the application runs smoothly on my personal computer, it encounters issues when deployed in a Docker environment due to invalid characters. The container being used is ubuntu:latest, along with python3, java, and ruby. ...

"Using Python with Flask framework to incorporate MatPlotLib for data visualization

I want to display a plot generated from a function in my Flask application without launching it outside of the browser. Although everything works fine, the plot opens in a separate window. I have been researching about IO but I'm still confused on ho ...

Extracting Job Titles from LinkedIn Profiles

Currently, my code is designed to search for job titles on LinkedIn (e.g. Cyber Analyst) and gather all the links related to these job postings/pages. The goal of the code is to compile these links into a list and then iterate through them to print out th ...

Python guide for load testing: calling various URLs

I need to run load testing on a web service that has multiple URLs using a code that is currently set up for a single URL. I want to create an array of URLs and have each thread hit all the URLs in the array. How can I modify my existing code to achieve ...

Discovering ANSI characters in Python: A comprehensive guide

I need help validating a series of strings to identify any illegal ANSI characters. I've come across information stating that extended ASCII is not the same as ANSI. I've been researching ways to determine if a character is an ANSI character, but ...

Inserting or changing a key-value pair prior to appending it to an array

Working on restructuring a JSON file to prepare for CSV conversion. The issue lies in the extra layer of nested lists within the JSON structure. The goal is to extract the Unique ID value associated with each data set along with the rest of the informatio ...

Python 3.5: exploring various elements within a given list

My task involves working with a list: myList = [abc123, def456, ghi789, xyz999] To search for particular values in myList, I have a designated "sub-list" of allowed values: allowed = [abc123, xyz999] Note: My objective is to verify if the elements in a ...

Regular expressions located and extracted successfully

I'm currently working on extracting the output of a match found using Regular Expressions. Here is the code I have: import re def findWholeWord(w): return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search match ...

What is the best method to retrieve all symbols and last prices from this JSON file?

My attempt at this solution didn't yield the desired results. Interestingly, it did work with a different JSON file (). I suspect the issue lies in the differences at the beginning and end of the current file, as there are some additional parts before ...