Why does my python text search work correctly for some rows in my csv file but not for others?


I wrote a python script that opens and reads a CSV file that has the following structure



It then writes a CSV file with the following structure



I also have the following python code



My script runs, the problem is that it only behaves like expected when reading certain lines of a CSV file. For some reason Python is only able to find the company name that I'm looking for in some of the rows of the CSV file.



I'll give a sample input file.



This is the output that I'm getting



I should be getting a value in the Consolidated Company column for every row of the output file since 'Good Company' shows up in every row of the file. However, what I'm actually seeing is that I'm only getting a value in some of the rows.



I haven't been able to figure out why my script works for some of the rows of my input file but fails for other rows of my input file. I would think that my script would either work for everything or fail for everything but it doesn't, why is that?



I modified your code and it works:



You did not wrote the second row in your version (Searched company) and you had at least a type in the if condition:



But the row is called 'Search Company'. You missed the "ed".



I think, that was the main reason for your problem. Also you should collect the rows and write them with .writerows().



My Output:



I discarded the headers, I know...



Edit: To include the headers, add



just before






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

The Dalles, Oregon

眉山市

清晰法令