我正在尝试以 txt 格式查找字符串,每次找到它时都会查找特定字符串以更改为另一个字符串并避免读取该行的第一个序列。
想象一下接下来的十六进制txt:
0000 09 06 07 04 00 00 01 00 1d 03 4b 2c a1 2a 02 01
0010 b7 09 01 47 30 22 a0 0a 80 08 33 04 03 92 22 14
0020 17 f0 a1 0b 80 00 81 00 84 01 00 86 00 85 00 83
0030 07 91 94 71 06 00 07 19
0000 09 06 07 04 00 00 01 00 2b 03 4b 27 a1 25 02 …Run Code Online (Sandbox Code Playgroud) 我想std::filesystem在我的项目中使用,这将允许我显示.txt当前目录中的文件(我使用 Ubuntu,我不需要 Windows 函数,因为我已经在 StackOverflow 上看到了一个)。
这是我的 GitHub 存储库:
https://github.com/jaroslawroszyk/-how-many-pages-per-day
我有一个解决这个问题的方法,如下所示:
void showFilesTxt()
{
DIR *d;
char *p1, *p2;
int ret;
struct dirent *dir;
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
p1 = strtok(dir->d_name, ".");
p2 = strtok(NULL, ".");
if (p2 != NULL)
{
ret = strcmp(p2, "txt");
if (ret == 0)
{
std::cout << p1 << "\n";
}
}
}
closedir(d);
}
}
Run Code Online (Sandbox Code Playgroud)
但是我在这里输入的代码想使用C++17,但我不知道如何找到文件.txt,现在我写道:
for (auto &fn …Run Code Online (Sandbox Code Playgroud) 假设我有一个文本文件,其中包含以下内容:
a
b
c
d
e
Run Code Online (Sandbox Code Playgroud)
我想迭代该文件的每一行,但在此过程中还要获取第一行后面的行。我已经尝试过这个:
with open(txt_file, "r") as f:
for line1, line2 in itertools.zip_longest(*[f] * 2):
if line2 != None:
print(line1.rstrip() + line2.rstrip())
else:
print(line1.rstrip())
Run Code Online (Sandbox Code Playgroud)
它返回类似:
ab
cd
e
Run Code Online (Sandbox Code Playgroud)
但是,我希望有这样的输出:
ab
bc
cd
de
e
Run Code Online (Sandbox Code Playgroud)
有人知道如何实现这一目标吗?提前致谢!
我正在 Win10 上使用 Julia 1.4.2 处理输出保存,我尝试了几种参考文档的方法。但对我来说没有任何作用。\n我想要保存的输出如下所示
\njulia> sim_result\nGpABC.SimulatedABCRejectionOutput(3, 1465, 20000, 1.0, [4.5398045285832636 16.341298256602684 0.8528241692701137; 2.2014098882111117 11.88704704892733 1.8060482719350048; \xe2\x80\xa6 ; 3.4569125171576727 14.206295598447817 1.2527525901911478; 1.1086538611233734 16.95205292134931 0.7814523160265465], [0.8718784407104312, 0.9897439825224637, 0.7361435718656393, 0.7863129137954922, 0.8097276891293583, 0.7167559341428585, 0.6600858843737456, 0.7420732257406872, 0.8085779131629843, 0.7603566788770011 \xe2\x80\xa6 0.9172504543539195, 0.5578993725435081, 0.8377191750525664, 0.7847440234430122, 0.6465487321194205, 0.716785821691151, 0.2596989086130145, 0.7210284576525666, 0.9855259563757373, 0.5775313512469311], [0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899 \xe2\x80\xa6 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899])\nRun Code Online (Sandbox Code Playgroud)\n它的结构是\n GpABC.SimulatedABCRejectionOutput(::Int64, ::Int64, ::Int64, ::Float64, ::AbstractArray{Float64,2}, ::AbstractArray{Float64,1}, ::StatsBase.Weights)\n我想将其保存 …
我有以下数据框:
text
1 "abc"
2 "def"
3 "ghi"
4 "jkl"
Run Code Online (Sandbox Code Playgroud)
如何将这些行合并到具有单行的数据框中,如下所示?
text
1 "abc, def, ghi, jkl"
Run Code Online (Sandbox Code Playgroud)
逗号分隔不是必须的,但所有值都应该位于一行中。这些值也可以存储在逗号分隔的字符串列表中。
我一直在尝试制作一个 python 脚本来使用 zipfile 模块压缩文件。虽然文本文件被做成了zip文件,但它似乎并没有压缩它;testtext.txt 是 1024KB,而 testtext.zip(代码的创建)也等于 1024KB。但是,如果我在文件资源管理器中手动压缩 testtext.txt,生成的 zip 文件将被压缩(具体为 2KB)。如果可能的话,我该如何克服这个逻辑错误?
下面是我用来(不成功)压缩文本文件的脚本。
from zipfile import ZipFile
textFile = ZipFile("compressedtextstuff.zip", "w")
textFile.write("testtext.txt")
textFile.close()
Run Code Online (Sandbox Code Playgroud) 当我运行下面的代码时,我得到以下回溯:
\nTraceback (most recent call last):\n File "C:\\demo\\test.py", line 11, in <module>\n pdf.output("splintered.pdf")\n File "C:\\demo\\lib\\site-packages\\fpdf\\fpdf.py", line 1065, in output\n self.close()\n File "C:\\demo\\lib\\site-packages\\fpdf\\fpdf.py", line 246, in close\n self._enddoc()\n File "C:\\demo\\lib\\site-packages\\fpdf\\fpdf.py", line 1636, in _enddoc\n self._putpages()\n File "C:\\demo\\lib\\site-packages\\fpdf\\fpdf.py", line 1170, in _putpages\n p = self.pages[n].encode("latin1") if PY3K else self.pages[n]\nUnicodeEncodeError: \'latin-1\' codec can\'t encode character \'\\u2019\' in position 74: ordinal not in range(256)\nRun Code Online (Sandbox Code Playgroud)\n我该如何解决?是因为我选择了 Arial 作为字体吗?我想做的就是将 txt 转换为 pdf 文件,因此如果有任何更简单的方法可以在 Python 中执行此操作,我将不胜感激。
\nimport fpdf\npdf = fpdf.FPDF(format=\'letter\')\n\ntxt = \'bees and butterflies. …Run Code Online (Sandbox Code Playgroud) 我试图将不同子文件夹中的几千个文件连接到一个文件中,并将每个连接文件的名称插入为第一列,以便我知道每个数据行来自哪个文件。本质上是从这样的事情开始:
编辑:我忽略了每个文件都有相同的标头,因此我相应地更新了请求。
Folder1
file1.txt
A B C
123 010 ...
456 020 ...
789 030 ...
Folder2
file2.txt
A B C
abc 100 ...
efg 200 ...
hij 300 ...
Run Code Online (Sandbox Code Playgroud)
并输出:
CombinedFile.txt
A B C
file1 123 010 ...
file1 456 020 ...
file1 789 030 ...
file2 abc 100 ...
file2 efg 200 ...
file2 hij 300 ...
Run Code Online (Sandbox Code Playgroud)
读完这篇文章后,我尝试了以下代码,但最终出现语法错误(抱歉,我对 awk 非常陌生!)
shopt -s globstar
for filename in path/**/*.txt; do
awk '{print FILENAME "\t" $0}' *.txt > CombinedFile.txt …Run Code Online (Sandbox Code Playgroud) 我想编写一个小函数,它将文件作为输入,并通过以下更改写入输出文件:
\r\n) 作为 EndOfLine ,则应仅用 LF ( \n) 替换。\n),则应将其替换为 CRLF ( \r\n)有关这方面的更多信息,请参阅这篇文章。
这是我这样做的尝试:
bool convertFile(string location) {
ifstream input;
ofstream output;
input.open(location);
if(!input.is_open()){
cout << "Invalid location!" << endl;
return false;
}
int dot = location.find_last_of('.');
if(dot != string::npos) location.replace(dot, 1, "_new.");
output.open(location);
char c;
for(;;){
input.get(c);
if(!input.good()){
if(input.eof()) return true;
else return false;
}
if(c == '\r'){
input.get(c);
if(c == '\n') output << '\n'; // \r\n …Run Code Online (Sandbox Code Playgroud)