473,414 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

How to print the incorrect words and line number from text file?

I have a list of incorrect words called # words[] and another list containing my txt file # text[].
I want to print the line number of the words located in the text.

I get the following error for my code.
invalid syntax: print(text, d[counter])

I am trying to print the words with its line number located in the text but it is not working its only printing the words anyone know why?

Below is the last bit of code start from d = {}
Expand|Select|Wrap|Line Numbers
  1. d = {}
  2. counter = 0
  3. for lines in words:
  4.     text = lines.split()
  5.     counter += 1
  6. if text not in d:
  7.     d[text] = [counter]
  8. if text in d:
  9.     d[text.append[counter]
  10.       print(text, d[counter])
May 24 '10 #1

✓ answered by bvdet

@lightning18
Your for loop does not do anything. I think your if statements (should be an if/elif block) should be indented to the same level as the for loop. Variable text is a list. You cannot use a list object as a dictionary key because it is mutable. I don't think you meant to do that anyway

Show us a sample of your data and a sample of your required output, and we can make some recommendations.

4 1760
bvdet
2,851 Expert Mod 2GB
@lightning18
Your for loop does not do anything. I think your if statements (should be an if/elif block) should be indented to the same level as the for loop. Variable text is a list. You cannot use a list object as a dictionary key because it is mutable. I don't think you meant to do that anyway

Show us a sample of your data and a sample of your required output, and we can make some recommendations.
May 24 '10 #2
Glenton
391 Expert 256MB
Often with invalid syntax it's the line before that's at fault.

In this case your line 9 needs at extra ]
May 24 '10 #3
dwblas
626 Expert 512MB
And your indentation is off.

We usually add keys to a dictionary in the following manner, eliminating the second if() statement (which should be an else BTW).
Expand|Select|Wrap|Line Numbers
  1. if next_word not in d:
  2.     d[next_word] = []     ## add new key pointing to an empty list
  3. d[next_word].append(counter) 
May 24 '10 #4
Glenton
391 Expert 256MB
@lightning18
Actually using defaultdict from the collections module is perfect for this kind of thing. I seem to remember doing this in a previous post? Was that for you?

Anyway it's something like this:
Expand|Select|Wrap|Line Numbers
  1. from collections import defaultdict
  2.  
  3. d=defaultdict(list)
  4. counter = 0
  5. for lines in words:
  6.     text = lines.split()
  7.     counter += 1
  8.     d[text].append(counter)
  9. for t in d:
  10.     print t,d[t]
  11.  
Mmm. Doing this made me realise just how many errors there were in your original code! e.g. d[counter] is not defined.
May 25 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
1
by: C#User | last post by:
hi, I am using vs2000, I want to print the code out with the line number, how to do it? Thanks!
2
by: Newbie | last post by:
In searching the web for a method to print out macro definitions, I found this code from Allen Browne to print out query definitions. Is there a similar way to print out macros? Public Function...
3
by: Dan Holmes | last post by:
Server stack trace: at IVS.Framework.ControlNumberService.InstallComponent(Identity id, ControlNumberInfo ctrlNumber) in...
2
by: John | last post by:
Hi For error logging purpose, is it possible to get from the vb system feature the SUB name and the line number? Thanks Regards
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
4
by: pooker75 | last post by:
I have a car.txt file and the first line is vin no., second line is model, third line is mfg, and 4th line is year. I may have an unlimited no. of vin numbers and I need the code to read the vin no....
11
by: Horacius ReX | last post by:
Hi, I have to search for a string on a big file. Once this string is found, I would need to get the number of the line in which the string is located on the file. Do you know how if this is...
1
by: sophie_newbie | last post by:
I'm sure this is exceedingly simple but I can't find it anywhere. When I catch an exception I would like to report the line number of the exception as well as the error info. try: someError()...
0
by: troy_lee | last post by:
Here's what I have so far... Private Sub Text4_AfterUpdate() Dim dtmWeek1BegDate As Long Dim dtmWeek1EndDate As Long Dim intWeekNumber As Integer ' Text4 is the beginning day of the month...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.