Jump to content

Hey All,

 

I have a .csv file with reviews for hotels. I need to take each of those reviews, and put them into the same string. Then convert it to a dictionary of words and the count of each word. I got it to work for a twitter assignment but now it keeps giving a 

TypeError                                 Traceback (most recent call last)
<ipython-input-1-dfe0abbe08b3> in <module>()
     13 for line in infile:
     14     print (line)
---> 15     text += line['text']
     16 
     17 words = text.split()

TypeError: string indices must be integers

 

I don't know where I am going wrong because it worked in one way and not the other. 

 

Here is my code

from collections import Counter
import csv

csvFile = open("FnLn-A2-Hotel1.csv", "a")
writer = csv.writer(csvFile)

text = ""
infile = open("HotelReviewSource.csv").readlines()
for line in infile:
#    print (line)
    text += line['text']

words = text.split()
cleantext = ""

for word in words:
    cleantext += word + " "
        
wordcount={}

for word in cleantext.split():
    if word not in wordcount:
        wordcount[word] = 1
    else:
        wordcount[word] += 1
print (wordcount)

c= Counter(cleantext.split())

for word,count in c.most_common():
    print (word,count)
    writer.writerow ([word,count])
    
csvFile.close()

 

CPU: Intel Core i7 8700k CPU Cooler: Corsair Hydro Series H100i Mobo:  Memory: G.Skill Ripjaws X 32GB 2133 Storage #1: 1TB 850 EVO SSD Storage #2: Western Digital Black 2TB Storage #3: Western Digital Green 4TB GPU: Gigabyte 980 Ti G1 Case: Mastercase5 PSU: EVGA 750 W G2 80+Gold Keyboard: Corsair K70 RGB Cherry MX Brown Mouse: Razer Deathadder Elite Monitor: LG 34UM94 Headset: Bose

Phone: Samsung Galaxy S9

Link to comment
https://linustechtips.com/topic/547759-more-python-help/
Share on other sites

Link to post
Share on other sites

im not exactly sure what youre doing wrong because you didnt say what is going in and what you want out, but I know the problem is that "line[x]" returns the character which is in position x of the string "line"

for example if line is "hello" then line[2] will return "n"

 

so you cant have line['text'] because 'text' isnt a number, its a string

 

if you could give more info on what infile looks like and what you want lines 13-15 to output that would help :)

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/547759-more-python-help/#findComment-7237722
Share on other sites

Link to post
Share on other sites

10 minutes ago, Enderman said:

im not exactly sure what youre doing wrong because you didnt say what is going in and what you want out, but I know the problem is that "line[x]" returns the character which is in position x of the string "line"

for example if line is "hello" then line[2] will return "n"

 

so you cant have line['text'] because 'text' isnt a number, its a string

 

if you could give more info on what infile looks like and what you want lines 13-15 to output that would help :)

Unless line is a dictionary , but it isn't here and it doesn't make any sense for it to be a dictonary.

 

Anyway, you should probably do this :

for line in infile:
    text.append(line)

Where text is a buffer ( vector of strings )

 

Or

text+=line

If you just want text to be a big string, but I don't see the point since you use split anyway.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/547759-more-python-help/#findComment-7237749
Share on other sites

Link to post
Share on other sites

8 minutes ago, Enderman said:

im not exactly sure what youre doing wrong because you didnt say what is going in and what you want out, but I know the problem is that "line[x]" returns the character which is in position x of the string "line"

for example if line is "hello" then line[2] will return "n"

 

so you cant have line['text'] because 'text' isnt a number, its a string

 

if you could give more info on what infile looks like and what you want lines 13-15 to output that would help :)

Sorry, the input from the source file is a list reviews. In cell A1 is a review for a hotel down to about A65 etc

 

I need the final output to be a counter of words and the number of times they show up.

the 67

best 54

hotel 18

when 65

etc

 

After 'text' becomes just one big long sting of all the reviews, the rest of the code should work.

2 minutes ago, Nineshadow said:

Unless line is a dictionary , but it isn't here and it doesn't make any sense for it to be a dictonary.

 

Anyway, you should probably do this :


for line in infile:
    text+=line

 

HOT DAMN! It worked!!

That was a HUGE thorn in my ass.

 

I think I want to put you on retainer for the rest of my semester haha

CPU: Intel Core i7 8700k CPU Cooler: Corsair Hydro Series H100i Mobo:  Memory: G.Skill Ripjaws X 32GB 2133 Storage #1: 1TB 850 EVO SSD Storage #2: Western Digital Black 2TB Storage #3: Western Digital Green 4TB GPU: Gigabyte 980 Ti G1 Case: Mastercase5 PSU: EVGA 750 W G2 80+Gold Keyboard: Corsair K70 RGB Cherry MX Brown Mouse: Razer Deathadder Elite Monitor: LG 34UM94 Headset: Bose

Phone: Samsung Galaxy S9

Link to comment
https://linustechtips.com/topic/547759-more-python-help/#findComment-7237789
Share on other sites

Link to post
Share on other sites

Just now, paps511 said:

Sorry, the input from the source file is a list reviews. In cell A1 is a review for a hotel down to about A65 etc

 

I need the final output to be a counter of words and the number of times they show up.

the 67

best 54

hotel 18

when 65

etc

 

After 'text' becomes just one big long sting of all the reviews, the rest of the code should work.

HOT DAMN! It worked!!

That was a HUGE thorn in my ass.

 

I think I want to put you on retainer for the rest of my semester haha

You know , errors are supposed to tell you what's wrong.

And here you had :

TypeError: string indices must be integers

At

 

text += line['text']

Which is pretty self-explanatory.

 

Anyway , good luck for the rest of your semester.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/547759-more-python-help/#findComment-7237806
Share on other sites

Link to post
Share on other sites

6 minutes ago, Nineshadow said:

You know , errors are supposed to tell you what's wrong.

And here you had :


TypeError: string indices must be integers

At

 


text += line['text']

Which is pretty self-explanatory.

 

Anyway , good luck for the rest of your semester.

The class is not really a coding class so much as it is data analysis. The professor just glosses over the coding aspect of it, but I'm making out a bit better than the rest which is not great..

CPU: Intel Core i7 8700k CPU Cooler: Corsair Hydro Series H100i Mobo:  Memory: G.Skill Ripjaws X 32GB 2133 Storage #1: 1TB 850 EVO SSD Storage #2: Western Digital Black 2TB Storage #3: Western Digital Green 4TB GPU: Gigabyte 980 Ti G1 Case: Mastercase5 PSU: EVGA 750 W G2 80+Gold Keyboard: Corsair K70 RGB Cherry MX Brown Mouse: Razer Deathadder Elite Monitor: LG 34UM94 Headset: Bose

Phone: Samsung Galaxy S9

Link to comment
https://linustechtips.com/topic/547759-more-python-help/#findComment-7237834
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×