1

I am outputting some data to a CSV file but for some reason the method alternates the way in which it outputs the data.The code below is the method being used to output the object to the CSV file.

@staticmethod
def OutputCSV(csv_file):
    posts = ApplicationModel.ApplicationModel.getTwitterObjects()
    csv_columns = ['post','sentiment']
    with open(csv_file, 'w') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(csv_columns)
        for TwitterObject.TwitterObject in posts:
            writer.writerow({
                TwitterObject.TwitterObject.post,
                TwitterObject.TwitterObject.sentiment
            })

The text below is a sample from the outputted CSV file.

post,sentiment

b'@Angel4Rubio @ehillm @Arsenal welcott been shit since he came to the league.',neg

pos,"b'Leicester closer to title, Arsenal held: Leicester City need just five points to complete a fairytale Premier ... '"

pos,"b'Leicester closer to title, Arsenal held: Leicester City need just five points to complete a fairytale Premier ... '"

pos,"b' @premierleague: ""I\'m slightly disappointed we didn\'t take all three points"" - Allardyce on #SUNARS\n\nMore:  '"

pos,"b'Leicester closer to title, Arsenal held: Leicester City need just five points to complete a fairytale Premier ... '"

b' @MesutOzil1088: best. team. \xf0\x9f\x92\xaa\xf0\x9f\x8f\xbc\xf0\x9f\x98\x8e\n#yagunnersya #BeTheDifference #AFCvLCFC #Arsenal #bigpoints ',pos

"b'Walcott, arteta, flamini, giroud and per to be sold. Bring in Hummells, Xhaka, Kante and Aubaumayang. #arsenal'",neg

pos,b' @FootieFansKnow: Amongst all the madness there is always Hector #Arsenal #Sunderland #Wenger #UCLDraw #Topfour  '
1
  • If you've got a preference for the order of the items, you should sort them into that order. See the sorted function. Commented Apr 24, 2016 at 19:40

2 Answers 2

5

Change

writer.writerow({
            TwitterObject.TwitterObject.post,
            TwitterObject.TwitterObject.sentiment
        })

to

writer.writerow([
            TwitterObject.TwitterObject.post,
            TwitterObject.TwitterObject.sentiment
        ])

set elements like dict keys are unordered. Hence the behavior. You can profile x in s for a large set s and compare it with x in l, where l = list(s). The look up in former is O(1), while in latter, O(n). This is one of the advantages of unordered hash lookups.

Sign up to request clarification or add additional context in comments.

Comments

1

Try with sorted:

@staticmethod
def OutputCSV(csv_file):
posts = ApplicationModel.ApplicationModel.getTwitterObjects()
csv_columns = sorted(['post','sentiment'])
with open(csv_file, 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(csv_columns)
    for TwitterObject.TwitterObject in posts:
        writer.writerow({
            TwitterObject.TwitterObject.post,
            TwitterObject.TwitterObject.sentiment
        })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.