Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding Sliced Positions
#1
Hello, I'm reading a large file line by line and I need to know if certain positions are greater than 0 if not, I don't want to print that line, it should then go to the next line. Example of line below.

Output:
000002250000000000089877000000000000.0002/28/202000000000.00000004698.45
for line in mf.readlines():
if(line[164:171] > 0)

....then print
 else next line
So if 4698.45 > 0

Would each position need to be assigned to a variable and then summed or is there a better way? If someone could point me in the right direction. Thanks!
Larz60+ write Nov-05-2020, 02:29 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Added for you this time, please use in all future posts,
Reply
#2
Okay so far. But when reading from a file, the positions are strings, so you'll have to attempt to cast it to a number to compare, or you'll have to ask if it has any digits other that zero.

for line in mf.readlines():
    if int(line[164:171]) > 0
        print(line)
Gizzmo28 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Read characters of line and return positions Gizzmo28 2 2,031 Nov-04-2020, 09:27 AM
Last Post: perfringo
  Update value in sliced dataframe bobopt 2 2,222 Nov-13-2019, 07:50 AM
Last Post: bobopt
  Print different positions in loop from functions konsular 5 2,697 Oct-16-2019, 08:10 PM
Last Post: buran
  Adding markers to Folium map only adding last element. tantony 0 2,120 Oct-16-2019, 03:28 PM
Last Post: tantony
  writing in particular positions clarablanes 5 2,818 Apr-13-2019, 04:07 PM
Last Post: clarablanes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020