我想把一个.txt文件转换成一个.csv文件。.txt文件包含一个单词列表以及它们的POS标记,其形式为: Sophie_NNP、listens_VBZ、to_TO、her_PRP$、classmates_NNS、as_IN、they_PRP、tell_VBP、her_PRP (等等)。
使用此代码,我将其转换为csv文件,以便每个单词及其标记都位于各自的行上:
import re
import pandas as pd
infile = 'POS_Test.txt'
Text_File = open(infile, 'r')
Table = 'POS_Text.csv'
output_file = open(Table, 'w')
for line in Text_File.readlines():
text_list = re.split('[ ]', line)
sentences = pd.DataFrame(text_list)
print(sentences, file=output_file)
output_file.close()这很好,只是问题是当我在Notepad++或Excel中打开Notepad++文件时,它似乎跳过了第30行到540行(总共有571行),并在一行中用点替换它们。
我是否可以在我的代码中添加一些东西来防止这种情况发生,以便我可以看到每一行?理想的情况是在Excel中,因为这是工作将继续的地方。
按照Jan的请求,以下是完整的.txt文件:
B_IN The_DT dare_NN Sophie_NNP listens_VBZ to_TO her_PRP$ classmates_NNS as_IN they_PRP tell_VBP her_PRP about_IN the_DT dare_NN ._。She_PRP is_VBZ nervous_JJ ._.She_PRP does_VBZ not_RB want_VB to_TO do_VB the_DT dare_NN ._.But_CC every_DT new_JJ pupil_NN has_VBZ to_TO do_VB it_PRP ._.Every_DT day_NN on_IN her_PRP$ way_NN to_TO school_NN,_,Sophie_NNP passes_VBZ the_DT old_JJ house_NN ._.It_PRP looks_VBZ terrifying_VBG and_CC everyone_NN says_VBZ it_PRP is_VBZ haunted_VBN ._.No_DT one_NN is_VBZ sure_JJ if_IN people_NNS live_VBP there_RB or_CC not_RB ._.People_NNS are_VBP too_RB scared_JJ to_TO find_VB out_RP ._.Most_JJS pupils_JJ never_RB knock_VBP on_IN the_DT door_NN ._.Only_RB brave_JJ children_NNS finish_VBP the_DT dare_NN and_CC knock_NN ._.again_RB :_:One_CD afternoon_NN a_DT young_JJ boy_NN is_VBZ bored_VBN ._.He_PRP wants_VBZ to_TO go_VB to_TO the_DT old_JJ house_NN ._.His_PRP$ parents_NNS tell_VBP him_PRP not_RB to_TO go_VB ._.They_PRP tell_VBP him_PRP it_PRP 's_VBZ dangerous_JJ ._.But_CC the_DT young_JJ boy_NN does_VBZ n_RB listen_VB ._.When_WRB he_PRP gets_VBZ to_TO the_DT house_NN he_PRP knocks_VBZ on_IN the_DT door_NN and_CC waits_NNS ._.Nothing_NN ._.He_PRP knocks_VBZ again_RB ._.Finally_RB,_,the_DT door_NN opens_VBZ ._.But_CC there_EX is_VBZ nobody_NN there_RB ._.The_DT boy_NN walks_NNS inside_IN ._.The_DT door_NN shuts_VBZ quickly_RB behind_IN him_PRP ._.The_DT house_NN is_VBZ dark_JJ and_CC it_PRP smells_VBZ disgusting_VBG ._.The_DT boy_NN hears_VBZ a_DT noise_NN ._.A_DT scratch_NN ._.He_PRP walks_VBZ along_IN the_DT hallway_NN ._.The_DT noise_NN gets_VBZ louder_JJR ._.Where_WRB is_VBZ it_PRP coming_VBG from_IN ?_.Suddenly_RB,_,he_PRP trips_VBZ ._.His_PRP$ head_NN hits_VBZ the_DT wooden_JJ floor_NN with_IN a_DT B_NNP A_NNP N_NNP G_NNP !_The_DT boy_NN lies_VBZ on_IN the_DT floor_NN,_,shocked_VBD ._.He_PRP hears_VBZ the_DT noise_NN again_RB ._.Scratch_NNP,_,scratch_NN,_,S_NNP C_NNP R_NNP A_NNP T_NNP C_NNP H_NNP,_,S_NNP C_NNP R_NNP A_NNP T_NNP R_NNP T_NNP C_NNP en25 20 en22 en24 en26#。The_DT boy_NN is_VBZ too_RB scared_VBN to_TO move_VB ._.He_PRP wants_VBZ the_DT scratching_NN to_TO stop_VB ._.Finally_RB,_,it_PRP does_VBZ ._.The_DT boy_NN slowly_RB stands_VBZ up_RP and_CC looks_VBZ around_RB ._.And_CC there_RB,_,in_IN front_NN of_IN him_PRP,_,is_VBZ a_DT dark_JJ figure_NN in_IN a_DT hood_NN ._.The_DT figure_NN stares_VBZ at_IN him_PRP ._.It_PRP says_VBZ nothing_NN ._.Terrified_NNP,_,the_DT boy_NN runs_VBZ to_TO the_DT door_NN ._.It_PRP 's_VBZ locked_VBN ._.He_PRP is_VBZ trapped_VBN ._.A_DT cold_JJ,_,white_JJ hand_NN grabs_VBD him_PRP ._.The_DT boy_JJ screams_NN ._.Nobody_NN ever_RB sees_VBZ the_DT boy_NN again_RB ._.People_NNS say_VBP that_IN sometimes_RB you_PRP can_MD still_RB hear_VB his_PRP$ screams_NN ._.Some_DT people_NNS say_VBP he_PRP is_VBZ dead_JJ ._.Others_NNS are_VBP not_RB so_RB sure_JJ ._.Sophie_NNP thinks_VBZ about_IN the_DT story_NN as_IN she_PRP walks_VBZ slowly_RB along_IN the_DT icy_NN path_NN ._.She_PRP reaches_VBZ the_DT garden_NN wall_NN ._.T_NNP H_NNP U_NNP M_NNP P_NNP,_,T_NNP H_NNP U_NNP M_NNP P._NNP T_NNP H_NNP U_NNP M_NNP P_NNP,_,_ U_NNP en26 20 en22 en24 en26#。She_PRP walks_VBD into_IN the_DT garden_NN ._.She_PRP shivers_VBD when_WRB she_PRP sees_VBZ graves_NNS by_IN the_DT path_NN ._.She_PRP tries_VBZ not_RB to_TO look_VB at_IN them_PRP as_IN she_PRP walks_VBZ up_IN to_TO the_DT house_NN ._.For_IN a_DT second_JJ Sophie_NNP thinks_VBZ she_PRP sees_VBZ a_DT dark_JJ figure_NN at_IN the_DT window_NN ._.She_PRP walks_VBZ slowly_RB to_TO the_DT door_NN ._.She_PRP knocks_VBZ and_CC waits_NNS ._.But_CC Sophie_NNP wants_VBZ to_TO be_VB really_RB brave_JJ,_,so_IN she_PRP knocks_VBZ again_RB ._.She_PRP waits_VBZ ._.After_IN a_DT while_NN,_,the_DT door_NN opens_VBZ ._.A_DT little_JJ old_JJ lady_NN looks_VBZ at_IN her_PRP ._.away_RB ._.I_PRP wonder_VBP why_WRB ._.anyway_RB ._.you_PRP are_VBP the_DT Then_RB she_PRP asks_VBZ Sophie_NNP to_TO come_VB in_IN ._.T_NNP H_NNP U_NNP M_NNP P_NNP,_,beats_VBZ faster_RBR ._.She_PRP looks_VBZ behind_IN her_PRP,_,then_RB walks_VBZ inside_RB ._.(“,”)
预期的解决方案只是上面屏幕截图中的内容,而不是省略号,你应该看到每个带有POS标签的单词--全部是571行。这是我现在得到的完整Excel表:Excel结果与省略号(希望.将通过实际的单词和POS标签替换)
发布于 2019-01-30 07:01:47
使用csv
您所看到的是显示在屏幕上的dataframe句子的中间。
import pandas as pd
import re
infile = 'POS_Test.txt'
Text_File = open(infile, 'r')
for line in Text_File.readlines():
text_list = re.split('[ ]', line)
sentences = pd.DataFrame(text_list)
sentences.to_csv('POS_Text.csv')发布于 2019-01-30 06:57:51
你需要用潘达斯做这个吗?似乎您可以将整个文本文件读入一个变量并在空格上拆分。
import csv
with open('POS_Test.txt', 'r') as Text_File:
split_values = Text_File.read().split(" ")
with open('POS_Text.csv', 'w') as output_file:
wr = csv.writer(output_file)
wr.writerow(split_values)编辑-更多细节:
首先,要用“with”打开文件。这将确保文件在您处理完文件后被关闭。
接下来是文件内容(大概是文本),您可以简单地对字符串执行.split(“")方法,该方法将在空格上拆分,并给出包含多个字符串的列表。
然后,我们再次用' with‘打开输出csv文件,并使用csv模块作家将文件写出来。
写字:将行中的所有元素(如上面所述的行对象的可迭代性)写入写入器的文件对象,按当前方言格式化。
发布于 2019-01-30 07:06:10
你可以把清单给熊猫DataFrame
with open('temp.txt') as f:
list = f.read().split()
df = pd.DataFrame(list)
df.to_csv('temp.csv')希望这能有所帮助!
https://stackoverflow.com/questions/54434624
复制相似问题