logo
down
shadow

Read only specific part first two lines from text file in python


Read only specific part first two lines from text file in python

By : user3042598
Date : November 28 2020, 12:01 PM
it fixes the issue Here's my problem description:- i have a .txt file to be read and i want only specific part of the first two lines to be read and printed , you can read line by line and find the necessary data:
code :
with open('yourTextFile.txt', "r") as f:
    for line in f:
        if "PID" in line or "SN" in line:
            dataArr = line.split(",")
            for data in dataArr:
                if 'PID' in data:
                    print(data)
                if 'SN' in data:
                    print(data)
PID: dd-ddddd-d        
 SN: qqqqqqqqq              

PID: yy-yyyyy-yy-yy    
 SN: ftyujnbghjn            


Share : facebook icon twitter icon
python - Read file from and to specific lines of text

python - Read file from and to specific lines of text


By : dr7ver
Date : March 29 2020, 07:55 AM
hope this fix your issue If you simply want the block of text between Start and End, you can do something simple like:
code :
with open('test.txt') as input_data:
    # Skips text before the beginning of the interesting block:
    for line in input_data:
        if line.strip() == 'Start':  # Or whatever test is needed
            break
    # Reads text until the end of the block:
    for line in input_data:  # This keeps reading the file
        if line.strip() == 'End':
            break
        print line  # Line is extracted (or block_of_lines.append(line), etc.)
How to read specific characters from lines in a text file using python?

How to read specific characters from lines in a text file using python?


By : Harsh Wardhan
Date : March 29 2020, 07:55 AM
this will help I would use regex but here is a version without, clearer than @Thrustmaster's solution imo.
code :
>>> text = "[class1] 1:-28 9:-315 13:-354227 2:-36.247 17:-342 8:-34 14:-3825"
>>> [int(x.split(':')[0]) for x in text.split()[1:]]
[1, 9, 13, 2, 17, 8, 14]
Python - Read specific lines in a text file based on a condition

Python - Read specific lines in a text file based on a condition


By : Anto
Date : March 29 2020, 07:55 AM
hope this fix your issue Problem Statement:
code :
import pandas as pd:
df = pd.read_csv('input_csv.csv') # This assumes you have a csv format file
names = {}
for name, subdf in df.groupby('name'):
    if name not in names:
        names[name] = {}
    if (subdf['date']==201505).any():
        if subdf['count'].count()==1:
            names[name]['status'] = 'new'
        else:
            names[name]['status'] = 'active'
            names[name]['last5median'] = subdf['count'].tail().median()
    else:
        names[name]['status'] = 'inactive'


>>>
{'John': {'status': 'inactive'},
 'Mary': {'last5median': 166.0, 'status': 'active'},
 'Tara': {'status': 'new'}}
python: editing specific lines in a text file.File not being read after first edit

python: editing specific lines in a text file.File not being read after first edit


By : Ramy Hosny
Date : March 29 2020, 07:55 AM
To fix the issue you can do The first problem is that 56 does not have a new line at the end. That means that it and the next line will be displayed on the same line. The second problem is that you are writing the string representation of the list onto one line instead of writing each string in the list on separate lines. Change lines[2] = 56 to lines[2] = "56\n", and change out.writelines(str(lines)) to out.writelines(lines)
How to read specific lines from a text file in python?

How to read specific lines from a text file in python?


By : parikshit jalihal
Date : March 29 2020, 07:55 AM
seems to work fine I have a text file that contains a lot of data. I want to be able to read the text file and write a new text file. However on the new text file I don't want it to include some part of the orginal.
Related Posts Related Posts :
  • Tuning the hyperparameter with gridsearch results in overfitting
  • some coordinates that I extracted from geocoder in Python are not saving in the variable I created
  • 7C in cs circles- python Im not sure what is wrong with this yet
  • How to fix 'AttributeError: 'list' object has no attribute 'shape'' error in python with Tensorflow / Keras when loading
  • python - thread`s target is a method of an object
  • Retrieve Variable From Class
  • What is the reason for matplotlib for printing labels multiple times?
  • Why would people use ThreadPoolExecutor instead of direct function call?
  • When clear_widgets is called, it doesnt remove screens in ScreenManager
  • Python can't import function
  • Pieces doesn't stack after one loop on my connect4
  • How to change font size of all .docx document with python-docx
  • How to store a word with # in .cfg file
  • How to append dictionaries to a dictionary?
  • How can I scrape text within paragraph tag with some other tags then within the paragraph text?
  • Custom entity ruler with SpaCy did not return a match
  • Logging with two handlers - one to file and one to stderr
  • How to do pivot_table in dask with aggfunc 'min'?
  • This for loop displays only the last entry of the student record
  • How to split a string by a specific pattern in number of characters?
  • Python 3: how to scrape research results from a website using CSFR?
  • Setting the scoring parameter of RandomizedSeachCV to r2
  • How to send alert or message from view.py to template?
  • How to add qml ScatterSeries to existing qml defined ChartView?
  • Django + tox: Apps aren't loaded yet
  • My css and images arent showing in django
  • Probability mass function sum 2 dice roll?
  • Cannot call ubuntu 'ulimit' from python subprocess without using shell option
  • Dataframe Timestamp Filter for new/repeating value
  • Problem with clicking select2 dropdownlist in selenium
  • pandas dataframe masks to write values into new column
  • How to click on item in navigation bar on top of page using selenium python?
  • Add multiple EntityRuler with spaCy (ValueError: 'entity_ruler' already exists in pipeline)
  • error when replacing missing ')' using negative look ahead regex in python
  • Is there a way to remove specific strings from indexes using a for loop?
  • select multiple tags by position in beautifulSoup
  • pytest: getting AttributeError: 'CaptureFixture' object has no attribute 'readouterror' capturing stdout
  • Shipping PyGObject/GTK+ app on Windows with MingW
  • Python script to deduplicate lines in multiple files
  • How to prevent window and widgets in a pyqt5 application from changing size when the visibility of one widget is altered
  • How to draw stacked bar plot from df.groupby('feature')['label'].value_counts()
  • Python subprocess doesn't work without sleep
  • How can I adjust 'the time' in python with module Re
  • Join original np array with resulting np array in a form of dictionary? multidimensional array? etc?
  • Forcing labels on histograms in each individual graph in a figure
  • For an infinite dataset, is the data used in each epoch the same?
  • Is there a more efficent way to extend a string?
  • Is it possible to do this loop in a one-liner?
  • invalid literal for int() with base 10: - django
  • Why does my code print a value that I have not assigned as yet?
  • the collatz func in automate boring stuff with python
  • How to find all possible combinations of parameters and funtions
  • about backpropagation deep neural network in tensorflow
  • Sort strings in pandas
  • How do access my flask app hosted in docker?
  • Replace the sentence include some text with Python regex
  • Counting the most common element in a 2D List in Python
  • logout a user from the system using a function in python
  • mp4 metadata not found but exists
  • Django: QuerySet with ExpressionWrapper
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org