Find A Character In A Comma Delimited Text File, And Return The String It Belongs To
I have a bunch of comma-delimited text files that I would like to read in, find the location of a special character "X", and return "Z" in the same line where it belongs to, in string types. The text file looks like this:
"a1", "a2", "a3"
"b1", "b2", "b3"
"c1", "c2", "c3"
...
"X", "Y", "Z"
...
"n1", "n2", "n3"
The number of lines where "X" appears varies file by file. So I would like to write a function that can apply to all files, returning "Z" as a string.
I have tried the following code:
from itertools import islice
def _function():
z = [0,0,0]
f = open(file, 'r')
num = sum(1 for line in f)
for i in islice(f, 0, num):
x = f.readline()
y = x.find('X')
if y == -1:
pass
else:
z = f.readline().rstrip().split(',')
return z[2]
However, when I run the code, I get 0 as my result, which means the code is not looping.
I also found that when I use f = open(file)
, it only lasts for one line of code.
My current version is Python 3.7 on Anaconda. Thanks!
Answer
Issue 1:
For y == -1
case you haven't provided any value for z
(not anywhere else either for that matter), only in the else
case.
So, what would be returned when getting the z[2]
when y == -1
?
In essense, you need to set a placeholder value for z
for that case or before that. A better place for a placeholder would be to set it before the for
loop so that you don't need to do the name binding on each iteration.
Issue 2:
You've already exhausted the file object iterator by counting lines:
num = sum(1 for line in f)
So when you do f.readline()
next inside the loop it would always return an empty string.
An easy fix would be to seek
back to the start of the file:
f.seek(0)
after num, and before entering the loop.
But thats still not Pythonic, the best way would be to iterate over the lines of the file (as file objects are iterators) using for
loop, and do you work:
for line in f:
y = line.find('X')
...
...
And of course, as open
is a context manager, use that to automatically handle the proper close
-ing of the file:
with open('file.txt') as f:
for line in f:
# Do stuffs
(Not going too much into your algorithm as that would be a question for code reviewing.)
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module