gpt4 book ai didi

python - CSV 输出顺序错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:02:55 24 4
gpt4 key购买 nike

我正在将一些数据输出到 CSV 文件,但由于某种原因,该方法改变了它输出数据的方式。下面的代码是用于将对象输出到 CSV 文件的方法。

@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
})

下面的文本是输出的 CSV 文件的示例。

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 '

最佳答案

改变

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

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

set 元素如 dict 键是无序的。因此行为。您可以分析大型集合 sx in s 并将其与 x in l 进行比较,其中 l = list(s) 。前者的查找是 O(1),而后者是 O(n)。这是无序哈希查找的优点之一。

关于python - CSV 输出顺序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36827977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com