logo
Tags down

shadow

Searching python text file without for loops and if statments


By : ihammerstrom
Date : July 31 2020, 08:00 PM
around this issue Reading a sequential file (i.e. a text file) is always going to be a sequential process. Unless you can store it in separate chunks or skip ahead somehow it will be hard to do any parallel processing.
What you could do is separate the inherently sequential reading process from the searching process. This requires that the file content be naturally separated into chunks (i.e. lines) across which the search is not intended to find a result.
code :


Share : facebook icon twitter icon

Python Beginners - Searching a text file for an array of text


By : user3673243
Date : March 29 2020, 07:55 AM
hop of those help? I'm brand new to python. I have a text file full of strings. I want to see which of these multiple strings contain a set of keywords. I've managed to write this: , Are you searching for lines with all keywords included?
code :
keywords = ['abc', 'def', 'ghi']

for line in f:
    if all(keyword in line for keyword in keywords):
        print line

Searching a text file for a line of text matching the text inputted from a user Python


By : Roel van Lieshoud
Date : March 29 2020, 07:55 AM
may help you . When the user enters a phrase, key words out of the phrase needs to be matched to text in a text file and then the line out of the text file can be printed back to the user. , First these 2 lines do not work as per your requirement:
code :
question = raw_input(" Welcome to the phone help center, What is the problem?")
if question in ["screen", "display", "blank"]:
question = raw_input(" Welcome to the phone help center, What is the problem?")
for k in  ["screen", "display", "blank"]:
    if k in question:
        for line in searchfile:
            if k in line:             # or maybe  if 'screen' in line ?
                print line
                break
        break

Writing to file and using if statments with loops


By : user412459
Date : March 29 2020, 07:55 AM
seems to work fine So, there are a couple of things wrong about your program. I'll try to cover them all here before I show you the changes.
code :
do {
    // collect data
    do {
        // save or edit
    } while (!validChoice);

} while (!done);
#include <stdio.h>
#include <string.h>

int get_line(const char *prompt, char *dest, size_t size) {
  printf("%s", prompt);
  fflush(stdout);
  if (fgets(dest, size, stdin) == NULL) {
    dest[0] = '\0';
    return 0;
  }
  dest[strcspn(dest, "\n")] = '\0';  // Lop off potential trailing '\n'
  return 1;
}

int main(void) 
{
    char first_name[20], surname[20], street_no[10], street_name[40], postcode[10], contact_no[20], save_edit_qu[10];
    int dd, mm, yy, done = 0;

    // Data collection loop: Runs as long as the user opts to edit the data.
    do {

        // Fetch data.
        get_line(" Enter first name:\n", first_name, sizeof first_name);
        get_line(" Enter surname:\n", surname, sizeof surname);
        get_line(" Contact Number\n", contact_no, sizeof contact_no);
        get_line(" Street Number\n", street_no, sizeof street_no);
        get_line(" Street Name\n", street_name, sizeof street_name);
        get_line(" Postcode\n", postcode, sizeof postcode);

        printf(" First Name : %s\n", first_name);
        printf(" Surname    : %s\n", surname);
        printf(" Contact No.: %s\n", contact_no);
        printf(" Street No. : %s\n", street_no);
        printf(" Stret Name : %s\n", street_name);
        printf(" Postcode   : %s\n", postcode);

        // Action loop: Runs as long as no valid input is given.
        do {
            get_line(" If the informations above is correct please enter SAVE/if you wish to change any informations please enter edit\n", save_edit_qu, sizeof save_edit_qu);

            // Option to quit.
            if (strcmp(save_edit_qu, "SAVE") == 0 || strcmp(save_edit_qu, "save") == 0) {
                fprintf(stdout, "Writing data to file...\n");
                // write data here.

                // Set done flag, and exit action loop.
                done = 1;
                break;
            }

            // Option to edit.
            if (strcmp(save_edit_qu, "EDIT") == 0 || strcmp(save_edit_qu, "edit") == 0 ) {

                //loop back to beginning of report
                break;
            }

            // Otherwise ask prompt again ^.
        } while (1);

    } while (!done);


    return 0;
}

How to break out of nested loops in Python when searching for a specific value in an Excel file?


By : user3573847
Date : March 29 2020, 07:55 AM
To fix this issue You have to put the logic inside while loop where you are taking the input and break from while loop when you find the shop address in file or based on your requirements.
code :
save = open('shop.txt', 'w')    
while not found:
    shop = int(input('Enter shop number: '))
    for row in ws.iter_rows(1):
        for cell in row:
            if cell.value == shop:
               print(ws.cell(row=cell.row, column=2).value, file = save)
               save.close()
               found = True
               # or you can write
               # break
        if found:
            break
    if not found:
        print('Invalid shop number!')

searching for strings in a text file using foreach loops


By : Tomi Rahmansyah
Date : March 29 2020, 07:55 AM
it helps some times Remove the assignement of N/A inside the loop and let it run until completition.
At the end you could just test the content of the textboxes to see if your loops have found something and, if not, set the N/A text
Related Posts Related Posts :
  • Print a table with aligned columns in python
  • PYTHON AND IBPY: UNEXPECTED EOF WHILE PARSING ; I can't figure it out
  • Entity Linking with spacy/Wikipedia
  • Question about "running times" of a for loop
  • Best way to .clean and .strip long string?
  • Jupyter Labs: Kernel Dies when Converting Tuple to PandasData Frame
  • Writing a CSV table from column variables?
  • Calling a function using a dictionary - dictionary keys as its parameters, dictionary values as its arguments
  • if error then do something else - string split
  • Filling values in a column
  • What are these set operations, and why do they give different results?
  • How can I run a function forever?
  • For loop resulting in wrong output
  • How to vectorize this simple NumPy function?
  • Matplotlib: creating a scatter plot where each point is colored (weighted) based on its count of instances in the datase
  • other possible way to duplicate a string from one variable in python
  • Cannot delete the column in DataFrame Pandas by del function
  • How to convert a string into ASCII?
  • Assigning values to a new column in Pandas
  • Django 3 - Making Model's FK Dropdown Display Current User's Data Only
  • Python Error of unsupported operand types for *: 'Nonetype' and 'Float' with array problem
  • Python in Jupyter Notebooks: 'Table' is not defined
  • Extract ID and value from set of strings in an array
  • finding the position of an element within a numpy array in python
  • Cannot run apache airflow after fresh install, python import error
  • Replace climbing sequence with its average
  • How to create a new column with a conditional count in a groupby pandas dataFrame
  • In python how to mix two sentence?
  • returning the 2nd letter in a string in a list and sorted the words by it. keep getting indexing range out of bound. Pyt
  • Convert string in an a tuple into an integer
  • Comparison using modifiable signs (python)
  • Python Beginning in new line for each letter in string
  • Byte to integer conversion
  • Why is 1e100+1 == 1e100 if ints do not have a maximum?
  • How to show list of certain items from JSON
  • How to fill a circle contour at the edge of an image?
  • How to BEST extract information from multiple dataframes based on a series of if\else conditions and matching values? (G
  • Convert string to few variables with pattern
  • Python Deleting Multiple Lines
  • How to replace a string based on first and last character
  • How can I resolve problem with tag_add in Tkinter
  • Check if csv file is empty in pandas
  • pd.NA vs np.nan for pandas
  • How to not display () and '
  • How to write a large dask array (numpy.ndarray) to a Zarr file leveraging GPUs?
  • How to crop circle image from webcam OpenCV and remove background
  • Can we login on a website with pure Python urllib, modify some form values, and submit? (without Selenium browser automa
  • Python: Fastest way of packing a 2d array of binary values into UINT64 array
  • Using global variable from one python file to another
  • How to add "<>" symbols to a string?
  • "IndexError: list assignment index out of range" in python
  • counting of elements (strings) in list python
  • if a certain columns's var is lower then n i want to drop that column
  • ValueError: Units 'M' and 'Y' are no longer supported, as they do not represent unambiguous timedelta values durations
  • How I get data in one column and put into a list using pandas?
  • What takes more memory, an 8 char string or 8 digit int?
  • I want to know what is the purpose of lambda x=x in the below code?
  • Pandas groupby select top N rows based on column value AND group size share
  • Appending to list produces None
  • Multiply List of RGB values Python
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org