我有一个包含中文字符的文本文件.
我想从该文本文件中读取一个随机行并显示它,这是我的代码:
from random import randint
num_lines = sum(1 for line in open('Chinese.txt'))
num_lines = num_lines - 1
choice = raw_input('How many times do you want to play?')
choice1 = int(choice)
while choice1 > 0:
random_number = (randint(0,num_lines))
with open('Chinese.txt') as inputFile:
bytes = inputFile.readlines()
unicodeText = bytes.decode('utf-8')
print unicodeText[int(random_number)]
choice1 = choice1 - 1
Run Code Online (Sandbox Code Playgroud) 我对这个简单的问题感到困惑.这是我的代码:
#include <iostream>
#include <string>
int main()
{
std::string str = "bb";
int counter;
for (counter = str.length() - 1; counter >= str.length() - 2; counter--)
{
std::cout << "counter: " << counter << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,它应该只打印两行,然后终止程序,但它保持打印行.事实上,循环不会在counter = -1处停止,这很奇怪!为什么会这样?
我的应用程序发送以[CR] + [LF]终止的NMEA字符串.NMEA标准规定了这种格式(例如来自陀螺罗盘的航向信息):'$ HEHDT,2.0,T*2D [CR] [LF]'.在接收端,字符串被丢弃为不完整.如何附加和发送这些字符?
发送很简单,只有几行代码(对象是Cp1tx:TIdUDPServer;):
...
Cp1tx.Active:= true;
Cp1tx.Broadcast(InStr,8051,'',IndyTextEncoding_8Bit);
Cp1tx.Active:= false;
Run Code Online (Sandbox Code Playgroud)
...
顺便说一下,我正在使用Delphi 10.1柏林.
为什么我的局部变量值的变化会反映到原始变量中?我传递给它的值在C++中.
#include <string>
#include <iostream>
void test(std::string a)
{
char *buff = (char *)a.c_str();
buff[2] = 'x';
std::cout << "In function: " << a;
}
int main()
{
std::string s = "Hello World";
std::cout << "Before : "<< s << "\n" ;
test(s);
std::cout << "\n" << "After : " << s << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Before : Hello World
In function: Hexlo World
After : Hexlo World
Run Code Online (Sandbox Code Playgroud) 我正在尝试结合grep并grep -v一起搜索.输出应显示所有以?结尾的行.xml,但要排除以?开头的行$.
这是我试过的命令; 没有工作:
grep *.xml file1.txt | grep -v '$' file1.txt > output
Run Code Online (Sandbox Code Playgroud)grep *.xml | grep -v '$' file1.txt > output
Run Code Online (Sandbox Code Playgroud)grep *.xml grep -v '$' file1.txt > output
Run Code Online (Sandbox Code Playgroud)grep *.xml '$' file1.txt > output
Run Code Online (Sandbox Code Playgroud)我有数据file.csv:
(...)
0000046;0000046;04688;29;1;52.1683;20.5567
0000046;0000046;04688;2A;1;52.1818;20.5639
0000046;0000046;04688;3;1;52.1785;20.5629
0000046;0000046;04688;4;1;52.1815;20.5638
0000046;0000046;04688;5;;52.1779;20.5635
0000046;0000046;04688;6;1;52.1813;20.5636
0000046;0000046;04688;7;;52.1777;20.5634
0000046;0000046;04688;8;;52.1810;20.5635
0000046;0000046;04688;9;1;52.1775;20.5631
0000046;0000046;05027;2;;52.1908;20.5660
0000046;0000046;05027;4;1;52.1907;20.5649
0000046;0000046;05527;1;1;52.1824;20.5636
(...)
Run Code Online (Sandbox Code Playgroud)
我需要提取第三个字段与给定值匹配的行.我试过了
cat file.csv |grep 05027
Run Code Online (Sandbox Code Playgroud)
不幸的是,这匹配任何包含05027任何地方的行 如何限制仅匹配第三个字段?
我是bash和Linux的新手.所以这可能是一个愚蠢的问题.
我正在尝试制作一个bash脚本来从网站下载多个文件.文件位于格式的URL中http://example.com/xyz/abc/2016/201601031400.tar.gz
请注意,文件名包含年,月,日期和时间.该数据贯穿了2007/01/01至2016/12/31的所有日期; 时间总是保持不变:"1400".
我想遍历日期范围中的每个日期,并希望下载所有tar.gz文件.
有人可以帮我解决循环部分并动态生成wget下载文件的URL吗?
我想防止除类的工厂之外的任何东西构造类类型的对象。我需要该类具有一个公共接口,但我想将其创建仅限于其工厂。我怎么做?
让我们称之为班级Car和工厂CarFactory。这是我在不使用friend所有私有成员并将其暴露给工厂的情况下如何执行此操作的最初想法:
class Car {
private:
Car();
Car(Car& ref);
friend class CarFactory;
};
class CarFactory {
public:
Car * makeCar();
};
Run Code Online (Sandbox Code Playgroud)
我发现了一个与Java有关的问题:如何使构造函数仅对工厂类可用?
上面的代码按原样工作。澄清一下,我想知道是否有一种方法可以只与工厂共享构造函数,而不能与所有私有成员共享?
假设我要输入2个命令行参数 - 源和目标.GetOptions通过仅检查参数名称的第一个字符而不是完整字符串来允许命令行.如何验证完整的参数字符串而不是仅允许其子字符串传递?
这是一个示例程序:
my ($source,$dest);
GetOptions(
'from=s' => \$source,
'to=s' => \$dest
) or die "Incorrect arguments\n";
Run Code Online (Sandbox Code Playgroud)
它接受以下任何一项:
-from-fro-fr-f
-to
-t但是,我希望它只接受
-from-to如果除了那些完整的词之外的任何东西都通过了
我如何禁止缩写选项?
看看他们的签名,这些看起来非常相似。ZIO 中这两个函数之间的语义差异是什么\xe2\x80\x99?
\n