从文件中读取单词并将其写入同一行(Reading Words from a File and Writing Them on The Same Line)
我有一个文件,每行包含一个单词。 每个句子用空行分隔。 我想读取文件并在同一行写出一个句子的全部单词。 例如:
This is a sample input Hello World !!
期望的输出是:
This is a sample input Hello World !!
我试试这个:
file = open('Words.txt', "r") Writfile = open('Sent.txt','w') for line in file: if line in ['\n']: Writfile.write('\n') else: Writfile.write(line + " ",)
I have a file that contains a word per a line. Each sentence is separated by an empty line. I want to read the file and write the whole words of a sentence on the same line. For example:
This is a sample input Hello World !!
The desired output is:
This is a sample input Hello World !!
I try this:
file = open('Words.txt', "r") Writfile = open('Sent.txt','w') for line in file: if line in ['\n']: Writfile.write('\n') else: Writfile.write(line + " ",)
原文:https://stackoverflow.com/questions/47720594
更新时间:2023-01-20 08:01
最满意答案
您可以使用索引来重新排序列。 例如,
In [119]: df = pd.DataFrame(np.arange(24).reshape(6,4), columns=list('ABCD')) In [120]: df Out[120]: A B C D 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 3 12 13 14 15 4 16 17 18 19 5 20 21 22 23 In [121]: df[list('CDAB')] Out[121]: C D A B 0 2 3 0 1 1 6 7 4 5 2 10 11 8 9 3 14 15 12 13 4 18 19 16 17 5 22 23 20 21
因此,只需使用
pd.read_table
像往常一样读取数据,然后使用重新排序列df = df[['col1', 'col2', ...]]
You could use indexing to reorder the columns. For example,
In [119]: df = pd.DataFrame(np.arange(24).reshape(6,4), columns=list('ABCD')) In [120]: df Out[120]: A B C D 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 3 12 13 14 15 4 16 17 18 19 5 20 21 22 23 In [121]: df[list('CDAB')] Out[121]: C D A B 0 2 3 0 1 1 6 7 4 5 2 10 11 8 9 3 14 15 12 13 4 18 19 16 17 5 22 23 20 21
So simply read in the data as usual using
pd.read_table
, and then reorder the columns usingdf = df[['col1', 'col2', ...]]
相关问答
更多-
以下内容将重新排列您的数据,以便在开头设置向量x中指定的列,然后在其原始顺序中设置其余列。 x <- c("C", "A", "T") mydata <- mydata[, c(x, setdiff(names(mydata), x))] 如果names列应保留在第一个位置且未在x指定,请使用(Thanks @StevenBeaupré指出并提供代码): mydata <- mydata[, c(names(mydata)[1], x, setdiff(names(mydata)[-1], x))] ...
-
如果我正确理解这个, append不是问题。 这只是关于列顺序。 要更改数据Dataframe列的Dataframe ,只需使用列名切片即可。 df1 = pds.DataFrame(columns=['var1', 'var2', 'var3']) # desired order # generate a disordered df somehow df_disordered = pds.DataFrame(columns=['var2', 'var1', 'var3']) df ...
-
当您使用Sp1不应引用Sp1 。 这总是会返回1 ,因此只返回你的第一行。 试试这个: > df[order(df$Sp1),] Sp1 Sp2 A03 2 1 A02 4 2 A01 7 6 > df[with(df, order(Sp1)), ] Sp1 Sp2 A03 2 1 A02 4 2 A01 7 6 Sp1 should not be quoted when you are using with. This would alw ...
-
按评级栏排序 df[with(df, order(-ratings)), ] Sort on ratings column df[with(df, order(-ratings)), ]
-
它不认为你的意思是在按频率排序后再次排序。 如果您想按频率重新排列行: sorted <- df[order(df$Freq, decreasing= TRUE),] It don't think you mean to sort again after ordering by frequency. If you want to rearrange the rows by frequency: sorted <- df[order(df$Freq, decreasing= TRUE),]
-
使用第二个数据帧作为参考来排序R数据帧列。(Order R dataframe columns by using second dataframe as a reference.)[2023-11-10]
让我们从创建两个示例数据框开始: patientID = c(123456789012345,1234,1234567890,123) state = c("FL","NJ","CA","TX") demog = data.frame(ID = patientID,state = state) clinical = data.frame(col1 = c(1,2,3), col2 = c(3,4,5), col2 = ... -
看起来你想在列上使用sort_index : df.groupby(['type', 'band']).mean().unstack(level=1)\ .sort_index(axis=1, ascending=False) 结果: val band C B A type 1 0.210 0.230 0.215 2 0.235 0.223 0.240 至于 ...
-
在差异+ iloc上尝试iloc : df = df.iloc[(df[input_class] - input_value).argsort()] df artist track class1 class2 class3 1 Yo La Tengo Our Way to Fall 0.14 0.86 0.0 2 Radiohead Fake Plastic Trees 0.03 0.97 0.0 ...
-
重新排序数据帧列(Reorder dataframe columns)[2022-03-16]
您可以使用索引来重新排序列。 例如, In [119]: df = pd.DataFrame(np.arange(24).reshape(6,4), columns=list('ABCD')) In [120]: df Out[120]: A B C D 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 3 12 13 14 15 4 16 17 18 19 5 20 21 22 23 In [121 ... -
你在函数中引用了一个引用,所以当你调用round你也修改了传入的df: In [7]: dat Out[7]: lat lon 0 6 1 1 7 2 2 8 3 In [10]: dat_new Out[10]: lat lon 0 6 1 1 7 2 2 8 3 如果您获取了传入的df的copy() ,那么您将看到现在所有值的比较显示为False : In [11]: def lonl ...