finding a pattern match and concatenating the rest of lines in python

2024/11/18 7:49:02

I have a small data set to clean. I have opened the text file in Pycharm. The data set is like this:

Code-6667+
Name of xyz company+ 
Address +
Number+ 
Contact person+
Code-6668+
Name of abc company, Address, number, contact person+
Code-6669+
name of company, Address+
number, contact person +

I need to separate the code lines and concatenate (or paste) the rest of the lines together till the next code line comes. This way I could separate my data into 2 fields, namely, the code of the company and secondly all the details all in one field. The eventual output being a table. The output should be something like this :

Code6667 - Company details 
Code6668 - Company details

Is there a way I could use a loop to do this? Tried this in R programming but now attempting it in Python.

Answer

Your question wasn't really clear, following a snippet to print out a line for each company, starting with "CodeXXXX - " and following with the other details.

with open(FILEPATH, 'r') as f:current_line = Nonefor line in f:line = line.strip()if line.startswith('Code-'):# new companyif current_line is not None:print(current_line)# create a line that starts with 'CodeXXXX - 'current_line = line.replace('-', '').replace('+', '') + ' - 'else:current_line += linecurrent_line += ' '

Output of your example code:

Code6667 - Name of xyz company+ Address + Number+ Contact person+ 
Code6668 - Name of abc company,Address, number, contact person+ 
https://en.xdnf.cn/q/120099.html

Related Q&A

Flat a list of containing only one dict

I have a dict which contain a list product which will contain only one dict: d = {"thickness": 90.0,"mass_surf": 37.8,"res_therm": 0.75,"codename": "codenam…

How to breakup a list of list in a given way in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Incrementing a counter while assigning it in python

Is it possible to do the following in python somehow?nodename = node%s % (++num if ct[0] != J else num)Or do I need to first do the increment and the assign it on the next line:if ct[0] != J:num += 1n…

Append two arrays together into one? (Numpy/Python)

I currently have an array of strings and Im trying to join it together with another array of strings to form a complete word to do some web parsing. For example:`Var1 [A B C, .... ....] Var2 …

Python 2.7 Isolate multiple JSON objects in a string

Ive to parse a text file that contains different kind of data. The most challenging is a line that contains three different JSON object (strings) and other data between them. Ive to divide the Json da…

pass different C functions with pointer arrays as the function argument to a class

I am trying to pass different functions which have pointers as arguments to a python function. One example of the input function as input parameter is the given normal function: Sample.pyxfrom cpython …

dynamic filter choice field in django

I am using django-filter to filter results on a page. I am able to use static choice filters like this:filters.pyFILTER_CHOICES = ( (, All States), (AL, Alabama), (AK, Alaska), (AZ, Arizona), #and so f…

How to print a table from a text file and fill empty spaces?

I have the following table in a txt file:|Client | Container weight | Country of Origin | Product code ||S4378 | 450 Ton | China | 457841 || | 350 Ton | Japan…

Open a file in python from 2 directory back

I want to read a file from 2 folders back.. with open(../../test.txt, r) as file:lines = file.readlines()file.close()I want to read from ../../ two folders back. but not work.. How i can do that ?

Do string representations of dictionaries have order in Python 3.4?

I know dictionaries themselves in Python do not have order. However, Im rather curious if when you call str() on a dictionary if it is always in the same order. It appears to be sorted (by key), no mat…